Yikes! This is what I was talking about. Amazing.

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 (<>) {
... } loop. So we get:

while (<>) {
print if !$seen{$_}++


Tries to lookup the line in the hash of lines we've already 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"


might return a 0 or "false" to indicate it wasn't found. Then the line


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.


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 the
'$_ 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!


Search Discussions

Discussion Posts


Follow ups

Related Discussions

Discussion Navigation
viewthread | post
posts ‹ prev | 7 of 16 | next ›
Discussion Overview
groupbeginners @
postedJul 30, '01 at 6:15p
activeJul 30, '01 at 9:40p



site design / logo © 2022 Grokbase