The outline of your code is sorta correct. The details show a few errors and chances for cleaning up. First, consider your code as reformatted by Perl Tidy:
#!/usr/bin/perl -w
open( IN, "$infile1" ) or die "Can't open $infile1" $linecount = 0;
%hash = 0;
while ( my $line = <IN> ) {
$linecount = $linecount + 1;
push( @{ $hash{$line} }, $linecount );
}
You may notice that the second line has a little more on it than you might first expect: you've omitted a semicolon.
Staying with the second line: it is much better to use the three parameter version of open: open IN, '<', $infile1 .... Checking the result with die is great, but you get more mileage from it if you report the actual error: ... die Can't open $infile1: $!";.
On line three you declare a hash using my - great stuff. However you don't need to assign anything to the hash to "initialize" it. Just my %hash; is just perfect in this case.
The while loop header on line four is just right.
Perl provides the special variable $. ($INPUT_LINE_NUMBER) that provides the line number of the most recently read line. Use that instead of maintaining your own line number count - if you actually need that count at all.
Line six is the key to the whole script. It adds the current line number to an array referenced by an entry in the hash keyed by the contents of the current line - not quite what you imply in your description, but fairly likely what you intend. Using the special variable line six can be cleaned up a little:
push @{ $hash{$line} }, $.;
However, now there is only one statement inside the while loop and that suggests that the while could be turned into a statement modifier. Consider:
push @{ $hash{$_} }, $. while <IN>;
and finally, it is good practice to close all file handles that you opened:
close IN;
Perl is environmentally friendly - it saves trees
|