would bemy ($server, @data) = (split(“,”,$line));
or even better would bemy ($server, $data) = (split(“,”,$line));
Actually this last addition of the 2 argument doesn't make too much difference since perl already optimizes this, and stops without parsing the whole line. Note also that none of the parentheses were needed on the right hand side.my ($server, $data) = split ',', $line, 2;
Now with a simple scalar data, it is easier to fix the code that you have to skip the first line, so instead of
you could tryif ($data[0] lt “!” ) { $data[0] = 0; } next if grep /[^0-9.]/, @data;
Furthermore, you did a clevernext if $data =~ /[^0-9.]/;
But perl already autoextends arrays, so you can simply writepush @{$usage{$server}}, 0 while @{$usage{$server}} < $files; push @{$usage{$server}}, $data[0];
This will leave all the previous zeros as missing, which you can deal with at the end more easily. BTW, your code will fill in missing zeros, if the are followed by data in later files, but if the missing zeros were in the third file, the arrays would not receive the zeros using your code.$usage{$server}->[$files] = $data unless $usage{$server}->[$files]
At the end you have
What is the point? Why not a cleancontinue { $files++ if eof; } close $fh or die "Can’t close file $file: $!";
my $files = 0; for my $file ("sfull1ns.dat","sfull2ns.dat","sfull3ns.dat") { open (my $fh,'<',$file) or die "Can’t open file $file: $!"; while (my $line = <$fh>) { ... } $files++ close $fh or die "Can’t close file $file: $!"; }
And you have no provision for output, obviously you need a final line after the whole looping is done which does
print "$_," . (join ',', @{$usage{$_}}) . "\n" for (keys %usage);
This will of course complain about uninitialized values in the array, but will run. Instead of zeros, the missing values will be missing, but we are nearly there. So now is the time to fix that by putting this code just before the printout.
You may argue that your solution was better, keeping this inside the main loop. It did take less code. But it was confusing in the middle of other processing. Here, to separate it out, in my mind makes cleaner code.for (keys %usage) { for my $f (0,1,2) { $usage{$_}->[$f] = 0 unless defined $usage{$_}->[$f]; } }
I have not put it all together for you, so that you can understand each piece as you implement it, but it did work for me.
In reply to Re: Eliminating Duplicate Lines From A CSV File
by b4swine
in thread Eliminating Duplicate Lines From A CSV File
by country1
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |