FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin for Harbour/xHarbour divide a text to array
Posts: 7317
Joined: Thu Oct 18, 2012 07:17 PM
divide a text to array
Posted: Mon Mar 21, 2016 02:39 PM

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

Since from 1991/1992 ( fw for clipper Rel. 14.4 - Momos)

I use : FiveWin for Harbour March-April 2024 - Harbour 3.2.0dev (harbour_bcc770_32_20240309) - Bcc7.70 - xMate ver. 1.15.3 - PellesC - mail: silvio[dot]falconi[at]gmail[dot]com
Posts: 117
Joined: Thu Jan 08, 2015 09:27 AM
Re: divide a text to array
Posted: Mon Mar 21, 2016 02:52 PM

Hello Silvio.Falconi,

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

Pieter

Posts: 7317
Joined: Thu Oct 18, 2012 07:17 PM
Re: divide a text to array
Posted: Mon Mar 21, 2016 03:01 PM

Characters

Since from 1991/1992 ( fw for clipper Rel. 14.4 - Momos)

I use : FiveWin for Harbour March-April 2024 - Harbour 3.2.0dev (harbour_bcc770_32_20240309) - Bcc7.70 - xMate ver. 1.15.3 - PellesC - mail: silvio[dot]falconi[at]gmail[dot]com
Posts: 117
Joined: Thu Jan 08, 2015 09:27 AM
Re: divide a text to array
Posted: Mon Mar 21, 2016 03:28 PM

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

Posts: 7317
Joined: Thu Oct 18, 2012 07:17 PM
Re: divide a text to array
Posted: Mon Mar 21, 2016 03:41 PM

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

Since from 1991/1992 ( fw for clipper Rel. 14.4 - Momos)

I use : FiveWin for Harbour March-April 2024 - Harbour 3.2.0dev (harbour_bcc770_32_20240309) - Bcc7.70 - xMate ver. 1.15.3 - PellesC - mail: silvio[dot]falconi[at]gmail[dot]com
Posts: 7317
Joined: Thu Oct 18, 2012 07:17 PM
Re: divide a text to array
Posted: Mon Mar 21, 2016 03:50 PM

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

Since from 1991/1992 ( fw for clipper Rel. 14.4 - Momos)

I use : FiveWin for Harbour March-April 2024 - Harbour 3.2.0dev (harbour_bcc770_32_20240309) - Bcc7.70 - xMate ver. 1.15.3 - PellesC - mail: silvio[dot]falconi[at]gmail[dot]com
Posts: 2064
Joined: Fri Jan 06, 2006 09:28 PM
Re: divide a text to array
Posted: Mon Mar 21, 2016 04:03 PM
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... :-)
Dios no está muerto...



Gracias a mi Dios ante todo!
Posts: 989
Joined: Thu Nov 24, 2005 03:01 PM
Re: divide a text to array
Posted: Mon Mar 21, 2016 04:37 PM
Code (fw): Select all Collapse
aText:= Array()
FOR i:= 1 TO Len( cMessage ) STEP 20
   aAdd( aText, PadR( SubStr( cMessage, i, 20 ), 20)  ) // Edited 
NEXT
Saludos
Carlos Mora
http://harbouradvisor.blogspot.com/
StackOverflow http://stackoverflow.com/users/549761/carlos-mora
“If you think education is expensive, try ignorance"
Posts: 117
Joined: Thu Jan 08, 2015 09:27 AM
Re: divide a text to array
Posted: Mon Mar 21, 2016 04:52 PM

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

Posts: 10733
Joined: Sun Nov 19, 2006 05:22 AM
Re: divide a text to array
Posted: Mon Mar 21, 2016 04:56 PM
Code (fw): Select all Collapse
   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
Posts: 989
Joined: Thu Nov 24, 2005 03:01 PM
Re: divide a text to array
Posted: Mon Mar 21, 2016 05:04 PM
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 (fw): Select all Collapse
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"
Posts: 7317
Joined: Thu Oct 18, 2012 07:17 PM
Re: divide a text to array
Posted: Mon Mar 21, 2016 05:09 PM

thanks to all ...I'm trying your samples

Since from 1991/1992 ( fw for clipper Rel. 14.4 - Momos)

I use : FiveWin for Harbour March-April 2024 - Harbour 3.2.0dev (harbour_bcc770_32_20240309) - Bcc7.70 - xMate ver. 1.15.3 - PellesC - mail: silvio[dot]falconi[at]gmail[dot]com
Posts: 117
Joined: Thu Jan 08, 2015 09:27 AM
Re: divide a text to array
Posted: Mon Mar 21, 2016 05:12 PM

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

Pieter

Posts: 117
Joined: Thu Jan 08, 2015 09:27 AM
Re: divide a text to array
Posted: Mon Mar 21, 2016 05:21 PM

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])

Posts: 7317
Joined: Thu Oct 18, 2012 07:17 PM
Re: divide a text to array
Posted: Mon Mar 21, 2016 05:22 PM

OK IT RUN OK THANKS

Since from 1991/1992 ( fw for clipper Rel. 14.4 - Momos)

I use : FiveWin for Harbour March-April 2024 - Harbour 3.2.0dev (harbour_bcc770_32_20240309) - Bcc7.70 - xMate ver. 1.15.3 - PellesC - mail: silvio[dot]falconi[at]gmail[dot]com

Continue the discussion