in reply to Reading Multiple lines

As presented, your code won't run:

   syntax error at mick2020.pl line 17, near "}"
   Execution of mick2020.pl aborted due to compilation errors.
which isn't a good start.

AFAIKS the problem is that before the if you've chomped the input line, but in the print after the if you don't restore the line ending. (Though you do at the end, inside the if.)

This appears to do what you seem to want:

while (<DATA>) { while (($_ !~ /"\s*$/) && !eof) { s/[\r\n]+$// ; $_ .= ' '.<DATA> ; } ; print $_; } __DATA__ "data", "data" ,"data", "data","data", "data" "data", "data" ,"data", "data with new line ", "and some more! ","data", "data" "data", "data" ,"data", "data","data", "data" "tada", "tada
Noting that the test for whether the line ends in " allows for all kinds of trailing whitespace on the line -- not just the line ending. Also, it only removes [\r\n] at the end of the lines it concatenates. Your code was smacking [\r\n] everywhere, except on lines that ended with " -- which may, or may not, have been deliberate.

Replies are listed 'Best First'.
Re^2: Reading Multiple lines
by mick2020 (Novice) on Oct 15, 2008 at 16:01 UTC
    Thanks for your input but unfortunately it has not worked.

    I have appended the code from the second part below.

    I think the problem is in the next phase. I can't understand though why it only processes the files that have the carriage returns using this preprocessing phase

    It processes a line (that has not being in the if statement)once and closes the filehandle but only on the lines that have not being processed in the if statement

    It could be a bug in my code(more than likely :) ) or a bug with perl.

      You've lost me.

      I assume that in the code in 717191 the PROCESSEDFILE is the file produced by stitching the CSV stuff together. I pushed the bits of code together, as best as I can, to get it to run. The $end variable was undefined and the $nameF variable was missing its my -- but apart from that it runs.

      First time through the while (<PROCESSEDFILE>) loop: $num will be set to 1, so if ($file{$key}{num} > 1) will fail, so no file will be opened. I cannot figure out what this is trying to do, but I suspect it's trying not to open a file in the '(column not present)' case -- and making a mess of it.

      Incidentally, the first time it gets a '(column not present)' case it will pass the unless ($file{$key}) test and promptly set my $nameF = $c[$field], although $c[$field] is already known to be undefined. Not sure I see the point of that, either.

      Finally, under all conditions -- including '(column not present)' and when it has failed to open a file -- it gets to the print {$file{$key}{name}} @c; line. What is this intended to do ? Under strict it gives me

        Can't use string ("/somewhere/data.END") as a symbol ref while "strict refs" in use ...
      
      but for all I know it does something wonderful in non-strict. I note however that you set $file{$key}{fh} which looks like a dead ringer for somewhere to output to ?

      Between you and me this looks like a bit of a train wreck. I suggest putting in the odd print statement here and there so that you can tell what's going on at each stage in the process... that may show you where things are and are not working as you expect.

      BTW: I recommend Markup in the Monastery