pldanutz has asked for the wisdom of the Perl Monks concerning the following question:
Better / shorter ways to write this? It takes a list and returns a hash of arrays (key => list of indices where key appears in argument)
use 5.012; use warnings; use Data::Dumper; sub occurrences { my %h; while (my ($i, $k) = each (@_)) { if (! $h {$k}) { $h {$k} = [] } push $h {$k}, $i } return %h } my %h = occurences (2, 1, 3, 4, 3, 4, 4, 5 ); print Dumper (\%h)'
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: array => hash of occurrence indexes
by BrowserUk (Patriarch) on Sep 11, 2013 at 11:17 UTC | |
Re: array => hash of occurrence indexes
by kcott (Archbishop) on Sep 12, 2013 at 05:19 UTC | |
Re: array => hash of occurrence indexes
by choroba (Cardinal) on Sep 11, 2013 at 11:20 UTC | |
by pldanutz (Acolyte) on Sep 11, 2013 at 11:22 UTC | |
by ww (Archbishop) on Sep 11, 2013 at 12:01 UTC | |
by pldanutz (Acolyte) on Sep 11, 2013 at 19:06 UTC | |
by ww (Archbishop) on Sep 11, 2013 at 23:13 UTC |
Back to
Seekers of Perl Wisdom