# Initialize the counter my %Counter = map { $_, [] } @keywords; while( my $line = ) # one line at a time { ... if( $line =~ /$keyword/ ) { # $. is the current input line number push @{ $Counter{ $keyword } }, $.; ... }