On Feb 5, 2004, at 11:56 AM, Gregg O'Donnell wrote:

Good follow-up, and here's a snippet:

Just FYI, there are multiple CSV parsing modules on the CPAN. I use Text::CSV_XS personally.


sub parse_line {
  my $line = shift;
  chomp($line);
  print "LINE: $line<br>\n" if $debug;
  my %record;
  my $entry;
  my $i = 1;      # First index
  while ($line) {
    if ($line =~
        s {
           ^\"
           ((?:[^\"]|\"\")*)
           \"
           (?:,|$)
          } {}x) {

" is not a special character, in a regular expression, so save your eyes and drop the \s. ;)


      $entry = $1;
    } elsif ($line =~
             s {
         ^
         (.*?)
         (?:,|$)
               } {}x) {
      $entry = $1;
    } else {
      die "Can't parse the line $line";
    }
    $entry =~ s/\"\"/\"/g;

This line is out of place, isn't it? It's only needed if the field was quoted and should be moved inside the if.


$record{$i++} = $entry;

You can eliminate the need for this line and the entry variable, if you store them when you find them.


Also, you're using a hash when you should be using an array. Numerically indexed data belongs in an array.

  }
  return \%record;
}

Unfortunately, this sub doesn't really tell us about your problem. I see nothing wrong here. Does %record contain what you think it does on exit? You might try printing it to find out.


I suspect the original problem is in your output code somewhere.

James


-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>




Reply via email to