Let me take a crack at the first one -- should be entertaining for everyone
;)
From: Jeff 'japhy/Marillion' Pinyan
Here's a one-liner:
perl -nle 'print if !$seen{$_}++'
The dash n (-n) puts the command 'print if !$seen{$_}++' in a while (<>) {perl -nle 'print if !$seen{$_}++'
... } loop. So we get:
while (<>) {
print if !$seen{$_}++
}
$seen{$_}
Tries to lookup the line in the hash of lines we've already seen.
$seen{$_}++
This is a complete guess, I can't seem to find anything like this in the
'Programming Perl' book.
It seems that if you say:
$seen{$_} = 1;
it causes the key to be added to the hash with the value 1, which is true in
boolean context.
So, if the key (line) wasn't previously seen, line"
$seen{$_}
might return a 0 or "false" to indicate it wasn't found. Then the line
$seen{$_}++
might take that 0/"false", increment it by one turning it to 1/"true"
causing the key/$_/"line" to be added with a value of 1/"true". If the
$_/"line" were already seen, it would have been added initially with a value
1/"true"; the ++ in this situation would just increment the value to
2,3,4...n, all of which are "true" values.
!$seen{$_}
Might negate the 1/"true" return of looking up a key that previously existed
in the hash, causing the
statement to execute, which is just short for
print STDOUT $_;
So how close am I and where can I read about this?
and here's another:
perl -pe '$_ x= !$seen{$_}++' (attributed to some of Larry's genius)
This would bypass the need for the print statement, but I'm not sure how theperl -pe '$_ x= !$seen{$_}++' (attributed to some of Larry's genius)
'$_ x= ' in the statement works.
and another, for use in a program
$seen{$_} ||= print OUT while <IN>;
Have fun. :)
This is tons of fun! Dying to know the answer!$seen{$_} ||= print OUT while <IN>;
Have fun. :)
Thanks,
David