divide a text to array
- Silvio.Falconi
- Posts: 4956
- Joined: Thu Oct 18, 2012 7:17 pm
divide a text to array
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
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
Re: divide a text to array
Hello Silvio.Falconi,
Maybe I can help you. What do you mean with cr?
Pieter
Maybe I can help you. What do you mean with cr?
Pieter
- Silvio.Falconi
- Posts: 4956
- Joined: Thu Oct 18, 2012 7:17 pm
Re: divide a text to array
Characters
I use : FiveWin for Harbour August 2020 (Revision) - Harbour 3.2.0dev (r1712141320) - Bcc7.30 - xMate ver. 1.15.3 - PellesC
Re: divide a text to array
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
"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
- Silvio.Falconi
- Posts: 4956
- Joined: Thu Oct 18, 2012 7:17 pm
Re: divide a text to array
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
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
- Silvio.Falconi
- Posts: 4956
- Joined: Thu Oct 18, 2012 7:17 pm
Re: divide a text to array
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
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
- joseluisysturiz
- Posts: 2024
- Joined: Fri Jan 06, 2006 9:28 pm
- Location: Guatire - Caracas - Venezuela
- Contact:
Re: divide a text to array
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...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
Dios no está muerto...
Gracias a mi Dios ante todo!
Gracias a mi Dios ante todo!
-
- Posts: 988
- Joined: Thu Nov 24, 2005 3:01 pm
- Location: Madrid, España
Re: divide a text to array
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"
Carlos Mora
http://harbouradvisor.blogspot.com/
StackOverflow http://stackoverflow.com/users/549761/carlos-mora
“If you think education is expensive, try ignorance"
Re: divide a text to array
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
I will think about it further.
Pieter
- nageswaragunupudi
- Posts: 8017
- Joined: Sun Nov 19, 2006 5:22 am
- Location: India
- Contact:
Re: divide a text to array
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
G. N. Rao.
Hyderabad, India
-
- Posts: 988
- Joined: Thu Nov 24, 2005 3:01 pm
- Location: Madrid, España
Re: divide a text to array
IF last element with less than 20 chars, Justadd sth likepieter 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
Code: Select all
If Len( aTail( aText ) ) != 20
aText[ Len(aText) ] := PadR( aText[ Len(aText) ], 20 )
ENDIF
Saludos
Carlos Mora
http://harbouradvisor.blogspot.com/
StackOverflow http://stackoverflow.com/users/549761/carlos-mora
“If you think education is expensive, try ignorance"
Carlos Mora
http://harbouradvisor.blogspot.com/
StackOverflow http://stackoverflow.com/users/549761/carlos-mora
“If you think education is expensive, try ignorance"
- Silvio.Falconi
- Posts: 4956
- Joined: Thu Oct 18, 2012 7:17 pm
Re: divide a text to array
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
Re: divide a text to array
Sorry, the code of Carlos seems to work now. (I don't know what happend before exactly).
Pieter
Pieter
Re: divide a text to array
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])
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])
- Silvio.Falconi
- Posts: 4956
- Joined: Thu Oct 18, 2012 7:17 pm
Re: divide a text to array
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