in reply to Using Map to Create AoA

Two people used
[ split " ", $arr->[$_] . " $_" ]
as part of their solution, but that's silly.
[ split(" ", $arr->[$_]), $_ ]
would be better.

Replies are listed 'Best First'.
Re^2: Using Map to Create AoA
by neversaint (Deacon) on Oct 17, 2005 at 01:26 UTC
    Dear ikegami,
    [ split(" ", $arr->[$_]), $_ ] would be better.
    Thanks for your response. But please correct me if I'm wrong. Your solution above does not give the index, theirs do. Here is the example:
    my $arr = ['A -4 C', 'C -4 B', 'B -4 A', 'A -2 C', 'C -3 B']; my @res = decomp_a2aoa_wth_idx($arr); sub decomp_a2aoa_wth_idx { my $arr = shift; return map { [ split (" ",$arr->[$_],$_) ] } (0 .. @{$arr}-1); } __END__ # Prints this result $VAR1 = [ ['A','-4','C'], ['C -4 B'], ['B','-4 A'], ['A','-2','C'], ['C','-3','B'] ];
    Update: Yes it was my fault. I made the typo. I apologize to ikegami for my carelessness. Thanks to Tanktalus for mentioning it.

    ---
    neversaint and everlastingly indebted.......

      ikegami's solution does - your typo doesn't. Note the location of the parenthesis - they are very important. IMO, this is an advantage of the AoH solution - it's very clear what is going on. ikegami's solution works to the original spec (I'm not so good at following specs - I like to make my code more flexible than many specs allow ;-}) as he presented it.