I think the others have answered your question about how to get the line number in the file-reading loop using $.. But your question also seems to be "How do I save off the value for later use?" Maybe I'm misunderstanding the question, or maybe my answer is blindingly obvious to you once you know about $., but here is an example (using your own example code) of how you might save off that info for later use when you process the data you collected:

while (<MYFILE>) { $alldata .= $_ ; if(/(\S+)\s+(\d+)/) { $mylist{$1} = {value => $2, linenum => $. }; } }

You see that instead of just saving off the right-hand value ($2) as a scalar value in the (badly-named) hash %mylist, the value is a hash-reference which contains the value and the line-number it was found on. You would then access the values along the lines of:

for my $key ( keys %mylist ) { my $value = $mylist{$key}{value}; my $linenum = $mylist{$key}{linenum}; print "$key has value $value, found on line $linenum\n"; }

HTH

--
edan


In reply to Re: Line number in a file by edan
in thread Line number in a file by ciryon

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.