Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??


First the switches: perl -anle"..."

  • -e"...": a small program to run immediately
  • -n: Feed the data (from the file(s) listed on the command line; or STDIN), to teh -e snippet above on line at a time in $_

    This has the effect of placing the -e"..." within a while loop very similar to this:

    while( <> ) { ... # the -e snippet here }
  • -l: auto chomp those lines on input; add a newline to print statements on output.
  • -a: autosplit those lines on whitespace (or the argument to -F if used) and place the results in @F

Now the -e snippet code in three parts:

  1. "$h{ $F[1] } = sprintf qq[%05u%s], $., $_;

    This builds %h with the second field of each input line as the key; and the whole line ($_) prefixed with the line number ($.) as the value.

    Later lines with matching second fields will overwrite earlier ones.

  2. }{

    This has the effect of converting the while loop shown above into:

    while( <> ) { ... # the part of the -e snippet before the }{ here; runs for ever +y line in the file. }{ # closes the while loop body and puts a bare block after it ... # The part of the -e snippet after the }{ goes here and is only + executed after the input is exhausted. }

    It a little like adding an END{} block containing the second half of the -e snippet.

  3.  print substr $_, 5 for sort values %h"

    One we've read all the input, only the last line with each second field value will remain in the hash, prefixed with the line number it came from.

    So sort the values from %h to get them into line number order and then print them out having removed the line numbers.

With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

In reply to Re^3: Data managing problem by BrowserUk
in thread Data managing problem by manorhce

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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others scrutinizing the Monastery: (4)
    As of 2018-04-24 17:17 GMT
    Find Nodes?
      Voting Booth?