in reply to Why does this hash remove duplicate lines
$lines{$_}++ is incrementing the value of the hash entry with the key of $_.
Since you're creating the key the first time you see a specific line, the value for that key is undefined. The first time you increment an undefined value in Perl you get 1.
print if not $lines{$_}++; Undefined values are false, and 1 or higher are determined to be true values in boolean context.
Basically, it's saying "Note this line. Note this line has been seen again if there's already a note about it. Print the line if it has not been seen before."
The reason order is preserved is simply because the printing is happening at the time the line is seen and the while loop and diamond operator are reading the lines in order.
As for whether you can use unless, that's probably simple enough for you to try and see, isn't it? ;-)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Why does this hash remove duplicate lines
by chromatic (Archbishop) on Mar 06, 2008 at 08:18 UTC | |
by mr_mischief (Monsignor) on Mar 06, 2008 at 17:19 UTC |