| Register | FAQ | Calendar | Search | Today's Posts | Mark Forums Read |
|
#1
|
| 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. |
|
#2
|
| 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. |
|
#3
|
| 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" 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. |
|
#4
|
| 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" 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. |
|
#5
|
| 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 In Genes situation he surely can simply use ALINES. Bye, Olaf. |
|
#6
|
| > 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. |
|
#7
|
| 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" 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. > |
|
#8
|
| 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. |
|
#9
|
| "MikeA" >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. |
![]() |
| Thread Tools | |
| Display Modes | |