in reply to Parsing a CSV file in a unique way

Check whether the newlines in fields are really CRLF ("\r\n"); they could be just "\n", while the record separator is "\r\n". Then you could set $/ = "\r\n" and do a s/\n/<br>/g over the fields.

If that wasn't the case, I'd do something like (untested)

my $sep = ';'; # field separator my $expected_field_count = scalar split /$sep/, <>; # header line while(<>) { chomp; my @fields = split /$sep/, $_; while(@fields < $expected_field_count) { my $fieldline = <>; my @l = split /$sep/, $_; $fields[-1] .= '<br>'. shift @l; push(@fields,@l) if @l; } ... }

That works only if your csv has a fixed number of fields. BTW, there are CSV modules out there...

--shmem

_($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                              /\_¯/(q    /
----------------------------  \__(m.====·.(_("always off the crowd"))."·
");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}

Replies are listed 'Best First'.
Re^2: Parsing a CSV file in a unique way
by herveus (Prior) on Oct 11, 2006 at 19:19 UTC
    Howdy!

    Fletch got it in one. I've used the same technique with no problems and great success. CSV is one of those areas where you can screw it up easily and get wrong results. Use the CPAN, Luke!

    yours,
    Michael
      Of course I use CPAN, herveus, and I included a CSV module search link in my previous post. But I'd learn less if I was running up to CPAN to let modules do my work everytime I hit a problem. IMHO, the way is: know how to solve the problem, then use CPAN modules to make your life easier...

      --shmem

      _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                    /\_¯/(q    /
      ----------------------------  \__(m.====·.(_("always off the crowd"))."·
      ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}