Taking ideas from Grandfather's code, and some ideas of my own, I have made some changes to your code that should work. I commented some lines you had and replaced them with new code. Mainly the biggest change was postponing the check for a missing average (and assignment of 0) until the printout part of the code (at the end) :-)
Chris
#!/usr/bin/perl use strict; use warnings; my %usage; my $files = 0; for my $file (qw/sfull1ns.dat sfull2ns.dat sfull3ns.dat/) { open (my $fh,'<',$file) or die "Can’t open file $file: $!"; while (my $line = <$fh>) { next if $line =~ /^Server/; #chomp($line); #my ($server, @data) = (split(",",$line)); # Assigns the first 2 values in the CSV # and discards the rest my ($server, $avg) = (split(",",$line)); #if ($data[0] lt "!" ) { # $data[0] = 0; #} #next if grep /[^0-9.]/, @data; #$usage{$server} = [] unless exists $usage{$server}; #push @{$usage{$server}}, 0 while @{$usage{$server}} < $files; #push @{$usage{$server}}, $data[0]; # if this is a second occurance of a server in the file, # its avg won't be assigned because the first one is already # stored there $usage{$server}[$files] ||= $avg; } continue { $files++ if eof; } close $fh or die "Can’t close file $file: $!"; } for my $server (sort keys %usage) { #Either this server has an average (for each element of the array) # or assign 0 to the ones that don't have a value my @avgs = map $usage{$server}[$_] || 0, 0..$#{$usage{$server}}; print join(",", $server, @avgs), "\n"; }

In reply to Re: Eliminating Duplicate Lines From A CSV File by Cristoforo
in thread Eliminating Duplicate Lines From A CSV File by country1

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.