Perl wrote:
> Very clean and short code!  Would you consider explaining how 
> this line works?  Especially how does it work on the second 
> and third iterations....
> 
> sort keys %{ { map { $_ => 1 } <FIN> } } ;

Read it from the inside out:

    <FIN>

This bit is in list context (due to the map), so it returns a list
consisting of each line from the file as a separate element

    map { $_ => 1 } <FIN>

This takes each line from the file and returns, for each line, the line and
1 as two separate elements. So ('foo', 'bar') would turn into ('foo' => 1,
'bar' => 1) or ('foo', 1, 'bar', 1).

    { map { $_ => 1 } <FIN> }

This uses the {} anonymous hash constructor which takes the list generated
previously (with lines and 1s interleaved) and turns it into a hash. It does
this by taking alternate elements as keys and values of the hash. Because
hash keys are unique, if you start with ('foo' => 1, 'bar' => 1, 'foo' =>
1), there'll only be one 'foo' in the resulting hash. The {} then returns a
reference to the resulting hash. This is the "de-duping" step.

    %{ { map { $_ => 1 } <FIN> } }

This uses %{ } to dereference the anonymous hash ref that we created
previously. The result is a hash (rather than a hashref).

    keys %{ { map { $_ => 1 } <FIN> } }

This returns the keys of the hash which was produced by dereferencing the
hashref. This returns, effectively, unique lines from the input file.
However, they are returned in seemingly random order.

    sort keys %{ { map { $_ => 1 } <FIN> } }

This sorts the keys alphanumerically. Now you have a sorted list of unique
entries in the file.

I'm not sure what you mean with "second and third iterations", though. This
sorts the whole file in one go. Or do you mean what happens if you run the
script on the file again? You get the same result, since de-duping doesn't
remove anything this time around, and sorting returns the lines back to the
original form.

Hope this helps.

Cheers,
Philip
_______________________________________________
Perl-Win32-Web mailing list
[EMAIL PROTECTED]
http://listserv.ActiveState.com/mailman/listinfo/perl-win32-web

Reply via email to