my %count; my $total; while () { chomp; my($src, $sport) = (split /;/)[9,12]; my $number_of_sport_shows_on_this_line = () = /$sport/gi; $count{$.} = $number_of_sport_shows_on_this_line; $total += $number_of_sport_shows_on_this_line; } # iterate %count to see how many the pattern shows up on each line # $total is the total number the pattern shows up in the file #### my(%count, $total); while () { chomp; my @fields = split /;/; my %seen; $seen{$_}++ for @fields; my $target = $fields[12]; $count{$.} = $seen{$target}; $total += $seen{$target}; }