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

Hi Monks!
I am trying using slice but I cant get it to work, any suggestions?
#!/usr/bin/perl use warnings; use strict; use Data::Dumper; my @new_order = (0, 1, 2, 3, 5, 4); my @AoA = ( [ "aaa", "bbb", "ccc", "ddd", "fff", "eee", ], [ "111", "222", "333", "444", "666", "555", ], [ "ppp", "eee", "rrr", "lll", "kkk", "ooo", ], ); my @new_aoa = @AoA[@new_order]; print Dumper \@new_aoa; print "\n\n";
Thank you!

Replies are listed 'Best First'.
Re: Change elements order in an array
by choroba (Cardinal) on Oct 16, 2015 at 15:04 UTC
    It works if you change the order in the first three elements - your array doesn't have elements with indices 4 and 5 (neither 3).
    my @new_order = (2, 1, 0);

    BTW, that's where the undef's were coming from.

    Update: to change the order of the inner arrays, you have to slice the inner arrays:

    my @new_aoa = map [ @$_[@new_order] ], @AoA;
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
      This is what I am trying to get it:
      I asked to wrong way.
      $VAR1 = [ [ 'aaa', 'bbb', 'ccc', 'ddd', 'eee', 'fff' ], [ '111', '222', '333', '444', '555', '666' ], [ 'ppp', 'eee', 'rrr', 'lll', 'ooo', 'kkk' ], ];

        Please see Re: Change elements order in an array. This node has been merged with it, and this one will be reaped.

        Per part of my post in Re: Change elements order in an array, you're wanting to modify the inner arrays. This will work. Just put your original data back in. It iterates over @AoA, and for each array reference within it, slices it to the specified order, then pushes those results within a new array reference onto the @new_aoa structure.
        my @new_order = (0, 1, 2, 3, 5, 4); my @AoA = ( [ qw(1 2 3 4 5 6), ], [ qw(a b c d e f), ], ); my @new_aoa; for (@AoA){ push @new_aoa, [@$_[@new_order]]; } print Dumper \@new_aoa;
Re: Change elements order in an array
by stevieb (Canon) on Oct 16, 2015 at 15:13 UTC
    What are you trying to achieve? Please provide example output. The way you're doing it now is re-ordering the top-level array only. By adding the appropriate number of elements to the top level array, you can see the results:
    #!/usr/bin/perl use warnings; use strict; use Data::Dumper; my @new_order = (0, 1, 2, 3, 5, 4); my @AoA = ( [ 'zero', ], [ 'one', ], [ 'two', ], [ 'three', ], [ 'four', ], [ 'five', ], ); my @new_aoa = @AoA[@new_order]; print Dumper \@new_aoa; __END__ $VAR1 = [ [ 'zero' ], [ 'one' ], [ 'two' ], [ 'three' ], [ 'five' ], [ 'four' ] ];
    If you're trying to re-sort the inner arrays, then the following should help, just replace the example data with your real structure... It iterates over @AoA, and for each array reference within it, slices it to the specified order, then pushes those results within a new array reference onto the @new_aoa structure.
    my @new_order = (0, 1, 2, 3, 5, 4); my @AoA = ( [ qw(1 2 3 4 5 6), ], [ qw(a b c d e f), ], ); my @new_aoa; for (@AoA){ push @new_aoa, [@$_[@new_order]]; } print Dumper \@new_aoa;
    -stevieb
      Thats what I was missing, "A loop".
      Thank you!