http://www.perlmonks.org?node_id=1055446


in reply to Re^2: Generating Hash-of-List using map?
in thread Generating Hash-of-List using map?

Oh, #@%$£"!

I admit defeat. A sort is probably not what the OP specified. One could build a full temporary hash in the map but that would be cheating as well (also relies on $a not been used for anything else before):

my %hash = map { /^([^-]*)-(.*)$/; exists $$a{$1}? (push(@{$$a{$1}},$2 +) and ()): ( $1=>$$a{$1}=[$2] ) } @CELLS;

So there is really no way around first declaring the hash and then filling it. What a pity!

Replies are listed 'Best First'.
Re^4: Generating Hash-of-List using map?
by LanX (Saint) on Sep 24, 2013 at 10:19 UTC
    > So there is really no way around first declaring the hash and then filling it. What a pity!

    O come on! Just "cheat"! ;-)

    DB<113> @CELLS = ("A-1", "B-2", "C-3", "A-4", "B-5", "C-6", "A-7", " +B-8", "C-9") => ("A-1", "B-2", "C-3", "A-4", "B-5", "C-6", "A-7", "B-8", "C-9") DB<114> %hash = map { /^([^-]*)-(.*)$/; if( $1 eq ($a//'-') ) { push + @$b, $2; () } else { ( $a = $1 => $b = [ $2 ] ) } } sort @CELLS; => ("A", [1, 4, 7], "B", [2, 5, 8], "C", [3, 6, 9])

    Cheers Rolf

    ( addicted to the Perl Programming Language)