in reply to Re^2: Reading Multiple lines
in thread Reading Multiple lines
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
use strict ; use warnings ; my $PROCESSEDFILE = '' ; open PROCESSEDFILE, '>', \$PROCESSEDFILE ; while (<DATA>) { while (($_ !~ /"\s*$/) && !eof) { s/[\r\n]+$// ; $_ .= ' '.<DATA> ; } ; print PROCESSEDFILE $_; } close PROCESSEDFILE ; open PROCESSEDFILE, '<', \$PROCESSEDFILE ; #use FileCache maxOpen => 50; # config: my $field = 0; my $sep = ","; my $end = '.END' ; $, = $sep; $\ = $/; my %file; my $fnum = 1; my $outDir = '/somewhere/' ; #$ARGV[1]; #unless (-d $outDir) { die "There is a no such directory." ; } #open PROCESSEDFILE, $ARGV[0] or die $!; while (<PROCESSEDFILE>) { chomp; print "processing '$_'\n" ; my @c = split(/$sep/, $_); my( $key, $num ) = defined $c[$field] ? ( $c[$field], $fnum++ ) : ( '(column not present)', +0 ) ; unless ($file{$key}) { my $nameF = $c[$field]; $nameF =~ s/"//g; $file{$key}{num} = $num; $file{$key}{name} = $ARGV[1].$nameF.$end; if ($file{$key}{num} > 1) { print "opening $file{$key}{name}" ; # -f $file{$key}{name} # and die "Sorry, '$file{$key}{name}' exists; won't clobber +."; ($file{$key}{fh} = 1) #cacheout $file{$key}{name} or die "Error opening '$file{$key}{name}' for write - $!"; } } print {$file{$key}{name}} @c; } __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
|
|---|