Apart from that, there are problems with your suggested code. It looks like your idea is for the hash value to be the line number in the file where a given text pattern is last seen, but you are post-incrementing $linecount when assigning it to each hash element (so the first line of the file is line number 0 instead of 1), and it won't work anyway, because you use "$line" as the hash key without ever assigning anything to $line -- in fact, if you tried "perl -cw" on your script as originally posted, it won't compile because $line is never declared.
(Lot's of monks post untested code and that's fine, but as a rule, when posting any snippet that includes "use strict;", you should at least pass it through "perl -cw" first to see whether it compiles.)
Update: by the way, to implement your idea (store the last line number containing a given text pattern), you don't need any variables other than the hash (and maybe the file name, but probably not even that):
#!/usr/bin/perl use strict; my %hash; $hash{$_} = $. while (<>); print "$hash{$_} : $_" for (sort {$hash{$a}<=>$hash{$b}} keys %hash);
In reply to Re^2: To assign values to a Hash
by graff
in thread hash values
by Abhisek
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |