Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Split ignores certain data in a Quoted CSV file.

by dukea2006 (Novice)
on Apr 06, 2010 at 18:09 UTC ( [id://833096]=perlquestion: print w/replies, xml ) Need Help??

dukea2006 has asked for the wisdom of the Perl Monks concerning the following question:

Arguably, this is a fairly easy question for someone with more Perl experience than I have. Issue: I have a quoted CSV file (created using MS Excel) that has data similar to the following: "John Smith","41160","California","3/31/2010" "John Adams","41166","Massachusetts","3/31/2010" "Samuel Adams","41170","Maine","3/31/2010" I'm using very simple Perl to load the data into an array and print out the fields (to ensure that the data was read correctly);

#!/usr/bin/perl # open file open(FILE1,"data.txt")or die ("Unable to open the file."); # read file into an array and close my @data = <FILE1>; close (FILE1); # Use a FOREACH loop to read through the data in the array foreach $caseMetrics (@data) { ($caseOwner,$caseNumber,$caseState,$openedDate, )=split(',', +$caseMetrics); print "$caseOwner\n"; print "$caseNumber\n"; print "$caseState\n"; print "$openDate\n"; }

What I find is that the array contains the all of the data EXCEPT the $openDate data. If I create a text file with the identical data the array does contain the $openDate data. NOTE: I am assuming that the data is not in the array because print "$openDate\n" doesn't return any data. So, my question is, do I have to do something special work with a CSV file? It seems odd that all of the fields are good with the exception of $openDate so, I am assuming that I am overlooking something fairly simple here. Thanks!

Replies are listed 'Best First'.
Re: Split ignores certain data in a Quoted CSV file.
by roboticus (Chancellor) on Apr 06, 2010 at 18:14 UTC

    dukea2006:

    $openedDate is not the same as $openDate. Use strict and warnings ... they are your friends!

      Oh man, that was it. THANK YOU Rototicus, I appreciate the help. I will be using strict and warnings from now on!

Re: Split ignores certain data in a Quoted CSV file.
by roboticus (Chancellor) on Apr 06, 2010 at 18:19 UTC

    dukea2006:

    There are a couple other things I should've mentioned:

    • Use <c> and </c> tags around your code, sample data, etc., to make your posts readable.
    • CSV files aren't as trivial as they should be, so you'll want to use a module like Text::CSV
Re: Split ignores certain data in a Quoted CSV file.
by toolic (Bishop) on Apr 06, 2010 at 18:21 UTC
    In addition to the advice to use strict and warnings to solve your problem, you might also consider using a CSV parser, such as Text::CSV_XS, because it can handle the more general case of embedded commas in your data.
      It would be immediately useful for the OP as it would handle the dequoting.

        ... and while it doesn't seem present in the example data, any enquoted commas will divide a field and offset the results in an unexpected manner.

Re: Split ignores certain data in a Quoted CSV file.
by AR (Friar) on Apr 06, 2010 at 18:20 UTC

    You should always use warnings and use strict. Had you done that, you would have seen that you assign to the variable $openedDate and try to print out $openDate.

    See the typo?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://833096]
Approved by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (2)
As of 2024-04-25 20:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found