Tielman Koekemoer (TNE) wrote:
I'm also very new to Perl and wrote a long and newbyish script
that
does exactly what the Unix command "sort FILENAME | uniq"
does just to
see how it can be done.
How long? Because you can do that on one line in perl. :-)
Hi John (we're not worthy - Wayne's World) ;-)
I am just standing on the shoulders of giants. :-)
I've just come back to your post above (30/05/05) and I've tried to
get the internal workings of the code to agree with my brain. Please
help!
perl -e'print sort grep !$seen{$_}++, <>' FILENAME
Initially I thought you were creating a hashref(?) key with every line
of input, if unique. But I cannot access the keys using the hashref
(below) so there is something else at work here.
The expression !$seen{$_}++ uses the hash %seen, if it was using a hashref it
would be written as !$seen->{$_}++
for $key_v (keys %{$seen}) {
print "$key_v\n";
}
This returns nothing when used with the above code in a script (code
posted at request). Sorry in advance if I've made newbie mistake but
please explain how !$seen{$_}++ knows what was used before.
Thomas wrote a pretty good explanation. If you have any more questions just
ask.
PS Will the Perl Cookbook have examples to common tools such as this?
Yes, it is described in section 4.7 "Extracting Unique Elements from a List"
(this is in the second edition.)
What does everyone think of the book - very helpful?
Yes it is a good book. A lot of the stuff in the book can also be found in
the FAQ files that are included with Perl.
perldoc -q "How can I remove duplicate elements from a list or array"
perldoc perlfaq
John
--
use Perl;
program
fulfillment
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>