divide a text to array

Post Reply
User avatar
Silvio.Falconi
Posts: 4956
Joined: Thu Oct 18, 2012 7:17 pm

divide a text to array

Post by Silvio.Falconi »

if I have a string sample :

Local cMessage:= "12345678901234567890123456789012345678901234567890123456789012345678901234567890"

I wish insert into a array records 20 cr sample

aadd( atextlinea,"12345678901234567890")

for a sample if there are 21 cr on cmessage the atextlinea must be 1

but I not Know the len of the cmessage and I wish create the atextlinea array

some can help me
I use : FiveWin for Harbour August 2020 (Revision) - Harbour 3.2.0dev (r1712141320) - Bcc7.30 - xMate ver. 1.15.3 - PellesC
User avatar
pieter
Posts: 117
Joined: Thu Jan 08, 2015 9:27 am

Re: divide a text to array

Post by pieter »

Hello Silvio.Falconi,

Maybe I can help you. What do you mean with cr?

Pieter
User avatar
Silvio.Falconi
Posts: 4956
Joined: Thu Oct 18, 2012 7:17 pm

Re: divide a text to array

Post by Silvio.Falconi »

Characters
I use : FiveWin for Harbour August 2020 (Revision) - Harbour 3.2.0dev (r1712141320) - Bcc7.30 - xMate ver. 1.15.3 - PellesC
User avatar
pieter
Posts: 117
Joined: Thu Jan 08, 2015 9:27 am

Re: divide a text to array

Post by pieter »

Oké,

"but I not Know the len of the cmessage..."
With LEN() you can calculated the length of a string
Local cMessage:= "12345678901234567890123456789012345678901234567890123456789012345678901234567890"
MsgInfo(LEN( cMessage ) ) //give 80

"for a sample if there are 21 cr on cmessage the atextlinea must be 1"
I am not sure if I understand this. Does it mean that if cmessage has 21 characters, the length of atexlinea array has to be 1? With the 21th character from cmessage on the first position of array atextlinea?

Pieter
User avatar
Silvio.Falconi
Posts: 4956
Joined: Thu Oct 18, 2012 7:17 pm

Re: divide a text to array

Post by Silvio.Falconi »

yes I know len function
But U perhaps not understood what I wish.
I explain you
I have a string of n character (I not Know the Len of this string)
I wish create a simply array with a string of 20 character each
sample :
aadd( atext,"12345678901234567890")
aadd( atext,"12345678901234567890")
aadd( atext,"12345678901234567890")

the strings must be 20 charactes
for a sample : I hace a string
len( cstring) := 41
I must create 3 record ( 2 of 20 cr and 1 of 1 cr)
but this one must be of 1 cr + space(19)
allway 20 cr
I use : FiveWin for Harbour August 2020 (Revision) - Harbour 3.2.0dev (r1712141320) - Bcc7.30 - xMate ver. 1.15.3 - PellesC
User avatar
Silvio.Falconi
Posts: 4956
Joined: Thu Oct 18, 2012 7:17 pm

Re: divide a text to array

Post by Silvio.Falconi »

for a sample :

if len(cMessage)==40
aadd(atext,Left( cmessage, 20 ))
aadd(atext,right(cMessage,20))
else
cTxt := Left( cmessage, 20 )
cTxt := cTxt + Space( 20 - Len( cTxt)
endif

but not run ok because I can have a big string
I use : FiveWin for Harbour August 2020 (Revision) - Harbour 3.2.0dev (r1712141320) - Bcc7.30 - xMate ver. 1.15.3 - PellesC
User avatar
joseluisysturiz
Posts: 2024
Joined: Fri Jan 06, 2006 9:28 pm
Location: Guatire - Caracas - Venezuela
Contact:

Re: divide a text to array

Post by joseluisysturiz »

Silvio.Falconi wrote:yes I know len function
But U perhaps not understood what I wish.
I explain you
I have a string of n character (I not Know the Len of this string)
I wish create a simply array with a string of 20 character each
sample :
aadd( atext,"12345678901234567890")
aadd( atext,"12345678901234567890")
aadd( atext,"12345678901234567890")

the strings must be 20 charactes
for a sample : I hace a string
len( cstring) := 41
I must create 3 record ( 2 of 20 cr and 1 of 1 cr)
but this one must be of 1 cr + space(19)
allway 20 cr
Creo que si cada vez que tomes los primeros 20 usas una VAR auxiliar donde vas restando los ya usados, esa VAR te controlara el tamaño restante, cuando te queden 21 el Len(VarAux) sera igual a 21, luego Len(Var_Aux) sera 1, luego creo podrias usar la funcion PAD para el relleno de espacios, es la idea que se me viene a mente...saludos... :shock:
Dios no está muerto...

Gracias a mi Dios ante todo!
Carlos Mora
Posts: 988
Joined: Thu Nov 24, 2005 3:01 pm
Location: Madrid, España

Re: divide a text to array

Post by Carlos Mora »

Code: Select all

aText:= Array()
FOR i:= 1 TO Len( cMessage ) STEP 20
   aAdd( aText, PadR( SubStr( cMessage, i, 20 ), 20)  ) // Edited 
NEXT
 
Last edited by Carlos Mora on Mon Mar 21, 2016 5:08 pm, edited 1 time in total.
Saludos
Carlos Mora
http://harbouradvisor.blogspot.com/
StackOverflow http://stackoverflow.com/users/549761/carlos-mora
“If you think education is expensive, try ignorance"
User avatar
pieter
Posts: 117
Joined: Thu Jan 08, 2015 9:27 am

Re: divide a text to array

Post by pieter »

Yes, I was also doing something with substr(), the code Carlos Mora has send works almost, but if I run this code, I see that there still is a problem at the end.
I will think about it further.

Pieter
User avatar
nageswaragunupudi
Posts: 8017
Joined: Sun Nov 19, 2006 5:22 am
Location: India
Contact:

Re: divide a text to array

Post by nageswaragunupudi »

Code: Select all

   local cMessage:= "12345678901234567890123456789012345678901234567890123456789012345678901234567890"
   local i,n
   local aTextLinea  := {}

   cMessage  := PadR( cMessage, ( n := Ceiling( Len( cMessage ) / 20 ) * 20 ) )
   for i := 1 to n step 20
      AAdd( aTextLinea, SubStr( cMessage, i, 20 ) )
   next
 
Regards

G. N. Rao.
Hyderabad, India
Carlos Mora
Posts: 988
Joined: Thu Nov 24, 2005 3:01 pm
Location: Madrid, España

Re: divide a text to array

Post by Carlos Mora »

pieter wrote:Yes, I was also doing something with substr(), the code Carlos Mora has send works almost, but if I run this code, I see that there still is a problem at the end.
I will think about it further.

Pieter
IF last element with less than 20 chars, Justadd sth like

Code: Select all

If Len( aTail( aText ) ) != 20
  aText[ Len(aText) ] := PadR(   aText[ Len(aText) ], 20 )
ENDIF
Or look at the EDITED source
Saludos
Carlos Mora
http://harbouradvisor.blogspot.com/
StackOverflow http://stackoverflow.com/users/549761/carlos-mora
“If you think education is expensive, try ignorance"
User avatar
Silvio.Falconi
Posts: 4956
Joined: Thu Oct 18, 2012 7:17 pm

Re: divide a text to array

Post by Silvio.Falconi »

thanks to all ...I'm trying your samples
I use : FiveWin for Harbour August 2020 (Revision) - Harbour 3.2.0dev (r1712141320) - Bcc7.30 - xMate ver. 1.15.3 - PellesC
User avatar
pieter
Posts: 117
Joined: Thu Jan 08, 2015 9:27 am

Re: divide a text to array

Post by pieter »

Sorry, the code of Carlos seems to work now. (I don't know what happend before exactly).

Pieter
User avatar
pieter
Posts: 117
Joined: Thu Jan 08, 2015 9:27 am

Re: divide a text to array

Post by pieter »

Code from Carlos (I only changed aText:= Array() to atext := { })

Local atext := { }
Local cMessage:= "abcdefghijklmnopqrstuvwxyz1234567890abcdefg"
MsgInfo(LEN( cMessage ) )// gives 43
FOR i:= 1 TO Len( cMessage ) STEP 20
aadd( aText, SubStr( cMessage, i, 20 ) )
NEXT
MsgInfo(aText[1])
MsgInfo(aText[2])
MsgInfo(aText[3])
User avatar
Silvio.Falconi
Posts: 4956
Joined: Thu Oct 18, 2012 7:17 pm

Re: divide a text to array

Post by Silvio.Falconi »

OK IT RUN OK THANKS
I use : FiveWin for Harbour August 2020 (Revision) - Harbour 3.2.0dev (r1712141320) - Bcc7.30 - xMate ver. 1.15.3 - PellesC
Post Reply