for $server(@servers) { my (@open, %found_total); # Initialize for each run open IN, $server; while () { # Keeps from loading the whole file in for $type($mediatypes) { if (/\.$type) { my $kbytes = (stat)[7]/1000; $found_total{$1} += $kbytes; push @output, ("$_ : $kbytes"); } } } # Output your @output array here if you want, # and/or %found_total (which contains the kbyte totals) }