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

In reply to Re: hash values by GrandFather
in thread hash values by Abhisek

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.