punkish has asked for the wisdom of the Perl Monks concerning the following question:
So, I am trying to learn one liners. I start with the following trivial script that loops over a DATA handle and prints out all the uniq words.
#!/usr/local/bin/perl while(<DATA>) { /(\w*)/; $i{$1}++; } print "$_\n" for keys %i; __DATA__ foo bar baz qux foo foo bar baz qux
Now, I want to convert this into a one liner, assuming the words are in a separate file called words.txt. So, I tried the following and failed
perl -nle '/(\w*)/;$i{$1}++;print for keys %i;' words.txt
The reason, of course, is that %i is not remembered from one iteration to the other. How do I create this one liner?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: global vars and one liners
by jethro (Monsignor) on Sep 15, 2009 at 01:16 UTC | |
|
Re: global vars and one liners
by AnomalousMonk (Archbishop) on Sep 15, 2009 at 01:09 UTC | |
by AnomalousMonk (Archbishop) on Sep 15, 2009 at 02:08 UTC | |
|
Re: global vars and one liners
by jwkrahn (Abbot) on Sep 15, 2009 at 08:34 UTC | |
by AnomalousMonk (Archbishop) on Sep 15, 2009 at 14:36 UTC | |
by ccn (Vicar) on Sep 15, 2009 at 19:21 UTC |