Page 1 of 1
divide a text to array
Posted: Mon Mar 21, 2016 2:39 pm
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
Re: divide a text to array
Posted: Mon Mar 21, 2016 2:52 pm
by pieter
Hello Silvio.Falconi,
Maybe I can help you. What do you mean with cr?
Pieter
Re: divide a text to array
Posted: Mon Mar 21, 2016 3:01 pm
by Silvio.Falconi
Characters
Re: divide a text to array
Posted: Mon Mar 21, 2016 3:28 pm
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
Re: divide a text to array
Posted: Mon Mar 21, 2016 3:41 pm
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
Re: divide a text to array
Posted: Mon Mar 21, 2016 3:50 pm
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
Re: divide a text to array
Posted: Mon Mar 21, 2016 4:03 pm
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...
Re: divide a text to array
Posted: Mon Mar 21, 2016 4:37 pm
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
Re: divide a text to array
Posted: Mon Mar 21, 2016 4:52 pm
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
Re: divide a text to array
Posted: Mon Mar 21, 2016 4:56 pm
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
Re: divide a text to array
Posted: Mon Mar 21, 2016 5:04 pm
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
Re: divide a text to array
Posted: Mon Mar 21, 2016 5:09 pm
by Silvio.Falconi
thanks to all ...I'm trying your samples
Re: divide a text to array
Posted: Mon Mar 21, 2016 5:12 pm
by pieter
Sorry, the code of Carlos seems to work now. (I don't know what happend before exactly).
Pieter
Re: divide a text to array
Posted: Mon Mar 21, 2016 5:21 pm
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])
Re: divide a text to array
Posted: Mon Mar 21, 2016 5:22 pm
by Silvio.Falconi
OK IT RUN OK THANKS