http://www.perlmonks.org?node_id=1020154


in reply to Re^2: Data managing problem
in thread Data managing problem

Sure

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

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.