XP is just a number PerlMonks

### sort hash of arrays of arrays

 on Oct 31, 2012 at 13:54 UTC Need Help??
maladav has asked for the wisdom of the Perl Monks concerning the following question:

Hi PerlMonks,

I'm working with the following data structure:

```#corrected code from first replies
my @inner1 = (0, 1, 2);
my @inner2 = (1, 4, 7);
my @inner3 = (2, 5, 8);
my @inner4 = (6, 7, 9);

my @array1 = (\@inner1, \@inner2, \@inner3, \@inner4);
my @array2 = (\@inner1, \@inner2, \@inner3, \@inner4);

my %hash = ('array1', \@array1, 'array2', \@array2);

So this is a 3-dimensional array which I'd like to sort numerically ascending by the 2nd elements of the innermost arrays, to get something like:

```(6, 7, 9)
(2, 5, 8)
(1, 4, 7)
(0, 1, 2)

This is what I was trying with no success (for my script I need to sort only one array at a time):

```foreach my \$i (sort { \$hash{array1}[\$b][1] <=> \$hash{array1}[\$a][1] }
+@{\$hash{array1}}) {

print @\$i[1], "\n";

}

When testing I get back the original order of elements in the middle array.
Can anyone help why?

Thanks!

Replies are listed 'Best First'.
Re: sort hash of arrays of arrays
by BrowserUk (Pope) on Oct 31, 2012 at 14:03 UTC
So this is a 3-dimensional array...

Not the way you shown it isn't:

```@inner1 = (0, 1, 2);
@inner2 = (1, 4, 7);
@inner3 = (2, 5, 8);
@inner4 = (6, 7, 9);

@array1 = (@inner1, @inner2, @inner3, @inner4);;

print @array1;;
0 1 2 1 4 7 2 5 8 6 7 9

@array2 = (@inner1, @inner2, @inner3, @inner4);;

print @array2;;
0 1 2 1 4 7 2 5 8 6 7 9

%hash = (array1, @array1, array2, @array2);;
pp \%hash;;
{
"0"      => 1,
"1"      => 4,
"2"      => 5,
"4"      => 7,
"5"      => 8,
"6"      => 7,
"7"      => 9,
"8"      => 6,
"9"      => "array2",
"array1" => 0,
}

If that's the way you are building it in your program; it is little wonder your attempt to sort it didn't work?

With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
font size=1

Ok, sorry, it's not a multi-dimensional array this way. In my script I build it up from a tab-delimited text file as follows:

```my %hash;

while (<FILE>) {

chomp;
my @line = split("\t");
push(@{\$hash{\$line[1]}}, [@line]);

}

This, at the end, is a 3-D array.
How can I do the sort based on the 2nd item of the innermost array?

Thank you,

How can I do the sort based on the 2nd item of the innermost array?

Okay. So you have a hash of arrays of arrays; something like this:

```{
1 => [
[1, 2, 3],
[6, 7, 8],
[16, 17, 18],
[26, 27, 28]
],
2 => [
[2, 3, 4],
[7, 8, 9],
[17, 18, 19],
[27, 28, 29]
],
3 => [
[3, 4, 5],
[8, 9, 10],
[18, 19, 20],
[28, 29, 30]
],
4 => [
[4, 5, 6],
[9, 10, 11],
[19, 20, 21],
[29, 30, 31]
],
}

The second item of which innermost array?

Each outer array contains multiple arrays. Which of those do you which to sort on? The first? The last? All of them?

With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
Re: sort hash of arrays of arrays
by Anonymous Monk on Oct 31, 2012 at 13:59 UTC
use Data::Dumper; and try dumping %hash to see what you have, because there is only one dimension in what you posted
Re: sort hash of arrays of arrays
by Anonymous Monk on Oct 31, 2012 at 14:04 UTC
```foreach my \$i (
sort {
### \$b is a reference not an index number
###     \$hash{array1}[\$b][1]
\$b->[1]
<=>
### \$a is a reference not an index number
###     \$hash{array1}[\$a][1]
\$a->[1]
}
@{\$hash{array1}}
) {
print @\$i[1], "\n";
}

Create A New User
Node Status?
node history
Node Type: perlquestion [id://1001665]
Approved by marto
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2018-04-21 00:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
My travels bear the most uncanny semblance to ...

Results (81 votes). Check out past polls.

Notices?