in reply to Re^2: Improving a script to search for keywords in a file
in thread Improving a script to search for keywords in a file

No need to feel guilty! You have come to the right place to learn though.

<INPUT> reads a line from the INPUT file handle. Generally you use open to create a file handle:

open INPUT, '<', 'theFileName';

The first ... is just a place holder for lines of code that you might insert at that point. In particular you are likely to chomp $line to remove the line end sequence from $line.

The second elipsis is a place holder for any lines of code that you might use to do further processing for the current line. Neither affects the important bit which is the hash access push @{ $Counter{ $keyword } }, $.;. That line pushes the current input line number onto the end of an array which is held onto by the counter hash (%counter). You can later list all the lines that contained each key word with something like:

print "$_ found in lines: @{$counter{$_}}\n" for sort keys %counter;

Note that $_ is the temporary variable and is an alias to each of the keyword values in %counter. @{$counter{$_}} causes each of the line numbers in the array held on to by the hash entry for the key word to be printed out with a space between them.

It's not expected that your code will be quite this suscinct on day 1. :) However, if you stick around here for a while and read a few of the other questions and answers, it'll start making sense pretty quick. Good luck, and enjoy Perl.


DWIM is Perl's answer to Gödel