neversaint has asked for the wisdom of the Perl Monks concerning the following question:

Dear Masters,
How can I transform my code below, to use map approach? In short, I want a compact and faster version of it.

Basically what the code does below is to break individual string from $arr into array then create an AoA with additional index inserted into it.
my $arr = ['A -4 C','C -4 B','B -4 A','A -2 C','C -3 B']; decomp_a2aoa_wth_idx($arr); sub decomp_a2aoa_wth_idx { my $arr = shift; my @aoa = (); foreach ( 0 .. @{$arr}-1 ) { my @tmp = split(" ", $arr->[$_]); push @tmp, $_; push @aoa,[@tmp]; } return @aoa; }
Results:
$VAR1 = [ ['A','-4','C',0], ['C','-4','B',1], ['B','-4','A',2], ['A','-2','C',3], ['C','-3','B',4] ];


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

Replies are listed 'Best First'.
Re: Using Map to Create AoA
by pg (Canon) on Oct 16, 2005 at 16:03 UTC

    There is no need to save the index as part of this, as that information is retained by the AoA itself. You have to make the best design decision "one truth is only stored once"

    use Data::Dumper; use strict; use warnings; my $arr = ['A -4 C','C -4 B','B -4 A','A -2 C','C -3 B']; my @a = map {[split " ",$_]} @$arr; print Dumper(\@a);

    This prints:

    $VAR1 = [ [ 'A', '-4', 'C' ], [ 'C', '-4', 'B' ], [ 'B', '-4', 'A' ], [ 'A', '-2', 'C' ], [ 'C', '-3', 'B' ] ];
      You have to make the best design decision "one truth is only stored once"
      Thanks for the very useful advice pg.

      My code above is just a small portion of a larger program I have. In this case I have to keep the index,
      cause I need to process the array, which will loose its order later. Subsequently I need to 'remember'
      those position to be able to callback certain value.

      You are welcome to see my recent postings and especially this, related and similar to this problems.

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

        Personally, I'd do this using an AOH. That keeps the data separate from the index, allowing the data to change format later without having to change other code (yes, you can use the -1 index, but all you have to do is use '3' as your index once to get confounded by later changes), and also allowing easy access to the original data without having to play with it:

        #!/usr/bin/perl -w use strict; my $arr = ['A -4 C','C -4 B','B -4 A','A -2 C','C -3 B']; my @a = do { my $idx = 0; map { { data => [ split ' ', $_ ], idx => $idx++, } }@$arr }; use Data::Dumper; print Dumper(\@a);
        This gives: If you really want it in a single AoA, then change the map like this:
        my @a = do { my $idx = 0; map { [ (split ' ', $_), $idx++ ], }@$arr };
        HTH

        If this is the case, then we do this:

        use Data::Dumper; use strict; use warnings; my $arr = ['A -4 C','C -4 B','B -4 A','A -2 C','C -3 B']; my @a = map {[split " ", $arr->[$_] . " $_"]} (0 .. @$arr - 1); print Dumper(\@a);

        Which gives:

        $VAR1 = [ [ 'A', '-4', 'C', '0' ], [ 'C', '-4', 'B', '1' ], [ 'B', '-4', 'A', '2' ], [ 'A', '-2', 'C', '3' ], [ 'C', '-3', 'B', '4' ] ];
Re: Using Map to Create AoA
by Dietz (Curate) on Oct 16, 2005 at 15:58 UTC
    map { split(" ", $arr->[$_]) } 0..$#{$arr} should do

    Update: Rereading your question this is not what you're seeking for. Following will save also the index.

    map { [ split(" ", $arr->[$_]." $_") ] } 0..$#{$arr};

    But as pg already said, there is no obvious advantage in storing the index.

      This gives a flat structure:

      $VAR1 = [ 'A', '-4', 'C', 'C', '-4', 'B', 'B', '-4', 'A', 'A', '-2', 'C', 'C', '-3', 'B' ];
        Right!
        Sorry for untested code
Re: Using Map to Create AoA
by ikegami (Patriarch) on Oct 16, 2005 at 22:35 UTC
    Two people used
    [ split " ", $arr->[$_] . " $_" ]
    as part of their solution, but that's silly.
    [ split(" ", $arr->[$_]), $_ ]
    would be better.
      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.