in reply to counting words in string
Simplest I could come up with is (more or less the same what you did)
$ perl -MDP -wE'my%x;$x{$_}++ for "iowq john stepy andy anne alic bert + stepy anne bert andy step alic andy" =~ m/(\w+)/g;DDumper\%x' { alic => 2, andy => 3, anne => 2, bert => 2, iowq => 1, john => 1, step => 1, stepy => 2 }
edit: and here the code inside the regex:
$ perl -MDP -wE'my%x;()="iowq john stepy andy anne alic bert stepy ann +e bert andy step alic andy" =~ m/(\w+)(?{$x{$^N}++})/g;DDumper\%x'
|
|---|