Discussion:
string to array
(too old to reply)
Kees
2004-04-21 11:06:02 UTC
Permalink
Hi
I'm a newbie on FoxPro and working with 5.0 (upgrade currently no option b.o. employer) and I'm having a hard time checking a comma delimited string. Best would be to validate with ALEN() but first I've got to transform the string into an array. Since there isn't a StringToArray() function or something of that kind available I'm breaking my head how to deal with it
Somebody can help me out? Does somebody know about an online tutorial available? Free documentation is hard to find.
Thnx
Holger Vorberg
2004-04-21 11:26:04 UTC
Permalink
Hi,

to pull the comma separated string into an array, you can use the ALINES()
function:

alines( aMyArray, cMyString, ",")


Another way is to parse the string using the WORDS() and WORDNUM() functions
from FOXTOOLS.FLL:

set library to foxtools.fll
for i = 1 to words( cMyString, ",")
? wordnum( cMyString, i, ",")
endfor
--
Holger Vorberg
MS Visual FoxPro MVP, Germany
Kees
2004-04-21 13:01:03 UTC
Permalink
Thanks for replying so quickly ;
I guess it's because of 5.0 that I'm not able to use neither of them since ALINES( ) is not recognised and I get an API not found error message on setting library to foxtools.fll (which is in fact available in C:\VFP)
Am I right that these are methods for VFP 6.0+ ?
Kurt Grassl
2004-04-21 15:23:10 UTC
Permalink
Thanks for replying so quickly ;)
I guess it's because of 5.0 that I'm not able to use neither of them since
ALINES( ) is not recognised
and I get an API not found error message on setting library to
foxtools.fll (which is in fact available in
C:\VFP).
Am I right that these are methods for VFP 6.0+ ?
ALINES -> yes VFP 6, with parameter ParseChar VFP7 or 8
FOXTOOLS.FLL -> I don't know

you could STRTRAN the commas to CHR(13) + CHR(10) and use MEMLINES() and
MLINE()

hth
Kurt

PS: you tried
set library to c:\vfp\foxtools.fll
?
:-)
Kees
2004-04-22 00:21:03 UTC
Permalink
Hi Kurt
Yeah, I tried SET LIBRARY TO C:\VFP\FOXTOOLS.DLL but that didn't help either
So I get my comma separated values one by one on a new line when printing STRTRAN(string,',',CHR(13)+CHR(16)) to screen but how do I get them all into an array? Do MEMLINES( ) and/or MLINE( ) serve for this purpose (i.e. I'm not familiar with these two functions)
Cheer
Andrew R
2004-04-22 09:24:02 UTC
Permalink
Kees,

The following function will do what you want.

Save the following code as say breakup.prg

set proc to <path>breakup.prg addi

lcC = 'item1;item2;item3'
dime laA[1]
? breakup(lcC, 1, 2, ';', @laA)

this will return 3, 3 items, and laA[1] = 'item1' laA[2] = 'item3' etc.

if you just wanted the third item

breakup(lcC, 3, 4, ';')

will return 'item3'

Andrew R.

<VFP CODE>

func BreakUp
LPara lcString, liFirst, liSecond, lcChar, laListArray
local luRet, liExchange, liCount, liI
do case
case pcount() = 1
liFirst = 1
liSecond = 2
case pcount() = 2
liSecond = liFirst + 1
endcase
if liFirst > liSecond
liExchange = liFirst
liFirst = liSecond
liSecond = liExchange
endif
if liFirst = liSecond or liSecond - liFirst > 1
liSecond = liFirst + 1
endif
if type('lcChar') != 'C' or (empty(lcChar) and lcChar != ' ')
lcChar = '~'
endif
lcString = iif(left(lcString, 1) != lcChar, lcChar, '') + lcString +
iif(right(lcString, 1) != lcChar, lcChar, '')
if right(lcString,1) <> lcChar
lcString = lcString + lcChar
endif
if pcount() = 5
if lcChar$lcString
* put into array
liCount = occurs(lcChar, lcString) - 1
dime laListArray[liCount]
for liI = 1 to liCount
laListArray[liI] =
substr(lcString,at(lcChar,lcString,liI)+1,at(lcChar,lcString,liI +
1)-at(lcChar,lcString,liI)-1)
endfor
luRet = liCount
else
luRet = 0
endif
else
if at(lcChar,lcString,liFirst) = 0 or at(lcChar,lcString,liSecond) = 0
luRet = ''
else
luRet =
substr(lcString,at(lcChar,lcString,liFirst)+1,at(lcChar,lcString,liSecond)-a
t(lcChar,lcString,liFirst)-1)
endif
endif
Return luRet
endfunc

</VFP CODE>

"Kees" <***@discussions.microsoft.com> wrote in message news:87BFE4A1-15AC-4DF9-8C77-***@microsoft.com...
: Hi Kurt,
: Yeah, I tried SET LIBRARY TO C:\VFP\FOXTOOLS.DLL but that didn't help
either.
: So I get my comma separated values one by one on a new line when printing
STRTRAN(string,',',CHR(13)+CHR(16)) to screen but how do I get them all into
an array? Do MEMLINES( ) and/or MLINE( ) serve for this purpose (i.e. I'm
not familiar with these two functions).
: Cheers
:
Kees
2004-04-22 22:01:05 UTC
Permalink
Excellent Andrew, I'm gonna try it in 5.0 but that's what I was looking for. Cheers
Kurt Grassl
2004-04-22 11:25:42 UTC
Permalink
Kees
Post by Andrew R
Hi Kurt,
So I get my comma separated values one by one on a new line when printing
STRTRAN(string,',',CHR(13)+CHR(16)) to screen but how do I get them all
CHR(16)? A typo? CHR(10)!
Post by Andrew R
into an array? Do MEMLINES( ) and/or MLINE( ) serve for this purpose
(i.e. I'm not familiar with these two functions).
cr = CHR(13) + CHR(10)
myString = "hasna" + cr + "lkj" + cr + "lkjoelk"

DIMENSION yourArray(MEMLINES(myString))

FOR i = 1 to MEMLINES(myString)
yourArray(i) = MLINE(myString, i)
ENDFOR

* --- optimized for long lists
_MLINE= 0
FOR i = 1 to MEMLINES(myString)
yourArray(i) = MLINE(myString, 1, _MLINE)
ENDFOR

hth
Kurt
Kees
2004-04-22 22:01:06 UTC
Permalink
Thanks Kurt, I guess it got too late but yes you did write CHR(10) indeed. I'll have it a try again. Thank you!
Marc Lyon
2004-04-22 19:20:30 UTC
Permalink
Kees,

Its SET LIBRARY TO C:\VFP\FOXTOOLS.FLL

Not DLL

HTH,

Marc
Post by Andrew R
Hi Kurt,
Yeah, I tried SET LIBRARY TO C:\VFP\FOXTOOLS.DLL but that didn't help
either.
Post by Andrew R
So I get my comma separated values one by one on a new line when printing
STRTRAN(string,',',CHR(13)+CHR(16)) to screen but how do I get them all into
an array? Do MEMLINES( ) and/or MLINE( ) serve for this purpose (i.e. I'm
not familiar with these two functions).
Post by Andrew R
Cheers
Kees
2004-04-22 22:01:03 UTC
Permalink
Sorry Marc, that was of course a typo. I did write it right but I suppose because of 5.0 it didn't do the job. Cheers.
Dan Freeman
2004-04-23 15:48:44 UTC
Permalink
Foxtools.fll has shipped with the product since FoxPro for Windows 2.x.
Someone must have deleted your copy.
Post by Kees
Sorry Marc, that was of course a typo. I did write it right but I
suppose because of 5.0 it didn't do the job. Cheers.
Olaf Doschke
2004-05-02 13:06:46 UTC
Permalink
Post by Kees
Yeah, I tried SET LIBRARY TO C:\VFP\FOXTOOLS.DLL but that didn't help
either.
FLL, not DLL.

Bye, Olaf.

Anders Altberg
2004-04-26 02:52:33 UTC
Permalink
You can populate a listbox with comma-delimited string.

o= create('listbox')
o.rowsourcetype=1
o.rowsource='Jan,Bill,May,Fred,Eve'
n = o.listcount
? n
o.sorted=.t.
dimension a (n)
for i = 1 to n
a(i)=o.list(i)
next
* o.visible=.t.

-Anders
Thanks for replying so quickly ;)
I guess it's because of 5.0 that I'm not able to use neither of them since
ALINES( ) is not recognised and I get an API not found error message on
setting library to foxtools.fll (which is in fact available in C:\VFP).
Am I right that these are methods for VFP 6.0+ ?
Loading...