Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

sort hash of arrays of arrays

by maladav (Initiate)
on Oct 31, 2012 at 13:54 UTC ( #1001665=perlquestion: print w/ replies, xml ) 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!

Comment on sort hash of arrays of arrays
Select or Download Code
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 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.

    RIP Neil Armstrong

    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,
      maladav

        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.

        RIP Neil Armstrong

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"; }
    See also Sorting dates with the Schwartzian Transform

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1001665]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2014-08-31 00:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (294 votes), past polls