in reply to global vars and one liners
... %i is not remembered from one iteration to the other.On the contrary, %i is remembered perhaps too well, being printed entirely for every line (iteration) in the file. Consider the following modification (my file words2.txt has the same data as given in the OP):
Maybe try something like:>perl -nle "/(\w*)/; $i{$1}++; print qq{$. $_} for keys %i;" words2.txt 1 foo 2 bar 2 foo 3 bar 3 baz 3 foo 4 bar 4 baz 4 qux 4 foo 5 bar 5 baz 5 qux 5 foo 6 bar 6 baz 6 qux 6 foo 7 bar 7 baz 7 qux 7 foo 8 bar 8 baz 8 qux 8 foo 9 bar 9 baz 9 qux 9 foo
Update: removed extraneous BEGIN { ... } block from second example.>perl -wMstrict -lne "/(\w+)/; $::i{$1}++; END { print qq{$_ $::i{$_}} for keys %::i } " words2.txt bar 2 baz 2 qux 2 foo 3
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: global vars and one liners
by AnomalousMonk (Archbishop) on Sep 15, 2009 at 02:08 UTC |