... %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
In reply to Re: global vars and one liners
by AnomalousMonk
in thread global vars and one liners
by punkish
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |