in reply to Change elements order in an array
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.#!/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' ] ];
-steviebmy @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;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Change elements order in an array
by Anonymous Monk on Oct 16, 2015 at 15:25 UTC |