my %lines_with_this_many_commas; while (<$csvfile>) { my $comma_count = tr/,/,/; $lines_with_this_many_commas{$comma_count}++; } print "Perl's getline counted $. lines\n"; print "with the following incidence of commas:\n"; my $PRINTF = "%10s %30s\n"; printf $PRINTF, 'Commas', 'Lines with this many commas'; for my $n (sort {$a<=>$b} keys %lines_with_this_many_commas) { printf $PRINTF, $n, $lines_with_this_many_commas{$n}; }