VFP9: Parsing a String

This is a discussion on VFP9: Parsing a String within the xbase forums in Other Databases category; Is there a function for parsing a comma-delimited string into an array of substrings? I thought there was, but I can not find it in the docs. Example: abc,,def,ghi, would return an array of elements: abc def ghi Sincerely, Gene Wirchenko Computerese Irregular Verb Conjugation: I have preferences. You have biases. He/She has prejudices....

Go Back   Database Forum > Other Databases > xbase

Database Forums

Register FAQ Calendar Search Today's Posts Mark Forums Read
  #1  
Old 06-18-2008, 11:58 PM
Default VFP9: Parsing a String

Is there a function for parsing a comma-delimited string into an
array of substrings? I thought there was, but I can not find it in
the docs.

Example: "abc,,def,ghi," would return an array of elements:
"abc"
""
"def"
"ghi"
""

Sincerely,

Gene Wirchenko

Computerese Irregular Verb Conjugation:
I have preferences.
You have biases.
He/She has prejudices.
Reply With Quote
  #2  
Old 06-19-2008, 02:29 AM
Default Re: VFP9: Parsing a String

On Wed, 18 Jun 2008 19:58:06 -0700, Gene Wirchenko
wrote:

> Is there a function for parsing a comma-delimited string into an
>array of substrings? I thought there was, but I can not find it in
>the docs.
>
> Example: "abc,,def,ghi," would return an array of elements:
> "abc"
> ""
> "def"
> "ghi"
> ""
>
>Sincerely,
>
>Gene Wirchenko
>
>Computerese Irregular Verb Conjugation:
> I have preferences.
> You have biases.
> He/She has prejudices.


ALINES has an option to specify the delimiter in newer versions.

There is also GETWORDNUM and GETWORDCOUNT.

However, you have to be careful about your requirement to get empty
items. GETWORDNUM/GETWORDCOUNT ignore empty items - they treat a
single delimiter the same as multiple delimiters in a row. I suppose
that comes from them originally only using space as the delimiter. I
think ALINES will keep the empty ones, but you need to check.
Reply With Quote
  #3  
Old 06-19-2008, 02:41 AM
Default Re: Parsing a String

Hi Gene:

If you don't have to worry about quotes or CRLF then you could easily use
STRTRAN. Otherwise, here are my comments:

Well, for starters, your example could be somewhat faulty and here's why.
True "comma delimted" as I'm sure you know actually uses quotes so as to
allow commas in a field. Therefore, your example being surrounded by quotes
(if that is how you intended it to be) would actually span only the first
field. I'm sure you probably know this but I'm just clarifying for those
who do not. So, "abc,,def,ghi," is very different from abc,,def,ghi,

If there could be quotes and/or CRLF then it can get really tricky. For one
thing, you could use an APPEND FROM with the CSV into a cursor with a lot
of fields. However, VFP does not like CRLF in fields and does not take that
into account with Append From. If that is the case they you have to write
your own function. In other words if the fields are say
FirstName,LastName,Notes and you have something like this:

Jon,Doe,A very nice person
Jane,Doe,"A nice person, but sometimes not so nice"
Jan,Smith,"Very nice person, and she works hard, and
she is a good programmer and most important of all
She loves VFP"
Jane,Smith,A nice person

So, in my CSV example I have embedded commas as well as CRLF in quotes. I'm
not sure what your situation is but for CRLF in quotes, you are going to
just have to write your own function. Should not be too hard but it could
be kind of a PITA especially if the file is more than 16 megs which requires
a low level read unless you just import the whole thing in a table and then
read the fields in a function. Yep, a PITA but not hard to program.just
your basic "dirt work" that you wish were in one easy function. Unless they
have changed the Append From CSV in VFP 9 (which I doubt) it's going to
require a function AFAIK.

Mike


If you are sure there will not be any quotes surrounding the fields then you
can easily use the STRTRAN function.

"Gene Wirchenko" wrote in message
news:aiij54pbe3fvklbcmsr0klsdantebs4skq-at-4ax.com...
> Is there a function for parsing a comma-delimited string into an
> array of substrings? I thought there was, but I can not find it in
> the docs.
>
> Example: "abc,,def,ghi," would return an array of elements:
> "abc"
> ""
> "def"
> "ghi"
> ""
>
> Sincerely,
>
> Gene Wirchenko
>
> Computerese Irregular Verb Conjugation:
> I have preferences.
> You have biases.
> He/She has prejudices.



Reply With Quote
  #4  
Old 06-19-2008, 02:53 AM
Default Re: VFP9: Parsing a String

Right - but with ALines you are stuck to only being able to process this
with a memofield and it won't work on large CSV files. Also, it won't work
if there are CRLF between quotes per my earlier reply. It can work but I
personally would probably go with StrTran if commas and CRLF are not an
issue and size of the file is less than 16 megs. Otherwise I think he is
stuck with either Append From CSV into a cursor or writing his own function
depending on the parameters of the CSV file.

Mike

"Jack Jackson" wrote in message
news:4arj54djc16qph0gj1m7t11gg7rq89maq2-at-4ax.com...
> On Wed, 18 Jun 2008 19:58:06 -0700, Gene Wirchenko
> wrote:
>
>> Is there a function for parsing a comma-delimited string into an
>>array of substrings? I thought there was, but I can not find it in
>>the docs.
>>
>> Example: "abc,,def,ghi," would return an array of elements:
>> "abc"
>> ""
>> "def"
>> "ghi"
>> ""
>>
>>Sincerely,
>>
>>Gene Wirchenko
>>
>>Computerese Irregular Verb Conjugation:
>> I have preferences.
>> You have biases.
>> He/She has prejudices.

>
> ALINES has an option to specify the delimiter in newer versions.
>
> There is also GETWORDNUM and GETWORDCOUNT.
>
> However, you have to be careful about your requirement to get empty
> items. GETWORDNUM/GETWORDCOUNT ignore empty items - they treat a
> single delimiter the same as multiple delimiters in a row. I suppose
> that comes from them originally only using space as the delimiter. I
> think ALINES will keep the empty ones, but you need to check.



Reply With Quote
  #5  
Old 06-19-2008, 03:23 AM
Default Re: Parsing a String

Well, ALINES is written originally for Texts and CRLF delimiter.
It's not intended to process CSV and respect a difference of
commas within strings and comma delimiters, therefore you
have APPEND FROM TYPE CSV.

In Genes situation he surely can simply use ALINES.

Bye, Olaf.


Reply With Quote
  #6  
Old 06-19-2008, 03:33 AM
Default Re: Parsing a String

> In Genes situation he surely can simply use ALINES.
PS: Mike, I see you're still stuck with VFP6 (SP3)

ALINES had no parameter in VFP6 to specify
other delimiters, but you can since VFP7.

Why don't you at least upgrade to SP5?

Bye, Olaf.


Reply With Quote
  #7  
Old 06-19-2008, 03:39 AM
Default Re: Parsing a String

I actually have VFP 9 but I have a lot of other priorities and my product is
installed in many locations so it will take some debugging before I upgrade
everything to 9 but it is on the agenda just not a high priority at the
moment compared to other items.

Thanks,
Mike

"Olaf Doschke" wrote in
message news:eV45Jad0IHA.4424-at-TK2MSFTNGP03.phx.gbl...
>> In Genes situation he surely can simply use ALINES.

> PS: Mike, I see you're still stuck with VFP6 (SP3)
>
> ALINES had no parameter in VFP6 to specify
> other delimiters, but you can since VFP7.
>
> Why don't you at least upgrade to SP5?
>
> Bye, Olaf.
>



Reply With Quote
  #8  
Old 06-19-2008, 04:52 AM
Default Re: Parsing a String

lcStrng = "abc,,def,ghi,"
lcDelim = ","
lcCRLF = chr(13) + chr(10)
alines( la, strtran( lcStrng + lcDelim, lcDelim, lcCRLF ) )

this works for me in VFP v6 (ignoring the quotes issue stated below).

Regards
Des


Gene Wirchenko wrote:
> Is there a function for parsing a comma-delimited string into an
> array of substrings? I thought there was, but I can not find it in
> the docs.
>
> Example: "abc,,def,ghi," would return an array of elements:
> "abc"
> ""
> "def"
> "ghi"
> ""
>
> Sincerely,
>
> Gene Wirchenko
>
> Computerese Irregular Verb Conjugation:
> I have preferences.
> You have biases.
> He/She has prejudices.



Reply With Quote
  #9  
Old 06-19-2008, 12:25 PM
Default Re: VFP9: Parsing a String

"MikeA" wrote:

>Right - but with ALines you are stuck to only being able to process this
>with a memofield and it won't work on large CSV files. Also, it won't work
>if there are CRLF between quotes per my earlier reply. It can work but I
>personally would probably go with StrTran if commas and CRLF are not an
>issue and size of the file is less than 16 megs. Otherwise I think he is
>stuck with either Append From CSV into a cursor or writing his own function
>depending on the parameters of the CSV file.


It is a string that I want to parse, not a file.

[snip]

Sincerely,

Gene Wirchenko

Computerese Irregular Verb Conjugation:
I have preferences.
You have biases.
He/She has prejudices.
Reply With Quote
Reply


Thread Tools
Display Modes



All times are GMT -4. The time now is 10:26 AM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Integrated by bbpixel2008 :: jvbPlugin R1013.368.1

Search Engine Friendly URLs by vBSEO 3.1.0
vB Ad Management by =RedTyger=
In an effort to better serve ads to our visitors, cookies are used on Mydatabasesupport.com. For more information, check out our Privacy Policy.