Help with split please - or maybe split isn't correct 
Author Message
 Help with split please - or maybe split isn't correct

Hi all,
    I have came across a dilemma that I haven't figured out yet.

I have some data
"01 2334567","04/12/1999",4.56,7.98,56,789.00,"This is a comment","This is
another, comment",456,N

And others like this in a file.

I need to separate it and get the "04/12/1999" and the 456 and if I split it
on the comma, thing go wacky and I can't reference specific fields in an
array to get
my values.

How can I split the above data line into an array with the following

some_array[0]     =    "01 2334567"
some_array[1]    =    "04/12/1999"
some_array[2]    =    4.56
some_array[3]    =    7.98
some_array[4]    =    56,789.00
some_array[5]    =    This is a comment
some_array[6]    =    This is another, comment
some_array[7]    =    456
some_array[8]    =    N

Any help would be appreciated,

Jack



Wed, 18 Jun 1902 08:00:00 GMT  
 Help with split please - or maybe split isn't correct

Quote:

> Hi all,
>     I have came across a dilemma that I haven't figured out yet.

> I have some data
> "01 2334567","04/12/1999",4.56,7.98,56,789.00,"This is a comment","This is
> another, comment",456,N

> And others like this in a file.

> I need to separate it and get the "04/12/1999" and the 456 and if I split it
> on the comma, thing go wacky and I can't reference specific fields in an
> array to get
> my values.

The format you are describing is technically known as CSV (Comma
Separated Values) format.  One of the problems with parsing it is that
it may contain commas inside fields as well as commas that separate
fields and records.  There are several modules that can correctly parse
this kind of data.    Text::CSV_XS and Text::ParseWords will both parse
the lines.  DBD::CSV and DBD::RAM will allow you to treat files with
that kind of data as databases and manipulate them with Structured Query
Language.  If you are going to do things like searching and sorting the
files, you may be better off taking one of these database approaches.

For simply parsing the line, this takes your string above and parses it
into an array of fields, correctly ignoring the comma that is inside the
quotation marks:

    use Text::ParseWords;

There is also further information about this problem in the perlfaq4 FAQ
section, titled
"How can I split a [character] delimited string except when inside
[character]? (Comma-separated files)"

--
Jeff



Wed, 18 Jun 1902 08:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Splitting on a line break that isn't

2. Is this correct split() behavior (sybperl too)

3. newbie.. split/join help please

4. Please help - split question?

5. SPLIT help please

6. Splitting on a number: help please

7. How to use split function to split on a backslash

8. split inside split - can it be done ?

9. PERLFUNC: split - split up a string using a regexp delimiter

10. Using split to return a @ without splitting on spaces

11. split on ':', but not '\:'

12. PERLFUNC: split - split up a string using a regexp delimiter

 

 
Powered by phpBB® Forum Software