in reply to global vars and one liners
Wrong reason. i don't know what you got when you tried your one liner, but you should have seen something like this:
foo foo bar foo bar baz ...
(because of the randomness of the hash not exactly in this sequence)
Reason is that your small for-loop is inside the while-loop created by -e.
To rectify this you could replace the inner loop with an 'if':
perl -nle '/(\w*)/; print if (not exists $i{$1}); $i{$1}++;' words.txt
If you really need to do the printing after the loop, you could use END (the seldom used opposite of BEGIN):
perl -nle '/(\w*)/;$i{$1}++; END{print for keys %i;} ' words.txt
|
|---|