Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Sorting data structure

by mhearse (Chaplain)
on Oct 14, 2005 at 02:58 UTC ( [id://500109]=perlquestion: print w/replies, xml ) Need Help??

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

I have a data structure which resembles the folloing:
$users{$value}->{count}
I need to sort this by the values store in count. Is there a way to sort hash references? Or another way to build this data structure, so that sorting can be performed? The hash refernece has many values, which could be considered user attributes. To iterated over them properly, it must be sorted by the key count. Would it be better to have:
$users[3]->{count}
Thanks.

Replies are listed 'Best First'.
Re: Sorting data structure
by davido (Cardinal) on Oct 14, 2005 at 03:02 UTC

    Something like this (assuming 'count' contains a numeric value):

    my( @sorted ) = sort { $users{$a}->{count} <=> $users{$b}->{count} } keys %users; print "$_ => $users{$_}->{count}\n" foreach @sorted;

    Dave

      And if it isn't numeric, replace the '<=>' (spaceship operator) with 'cmp' .

      ...reality must take precedence over public relations, for nature cannot be fooled. - R P Feynmann

Re: Sorting data structure
by ioannis (Abbot) on Oct 14, 2005 at 05:11 UTC
    The variable users, as used in the original post, represents a hash, not an array. This we know from the brace after the -> infix operator; therefore, assuming numerical comparison, the answer to the original question is this:
    @users = sort { $::a->{count} <=> $::b->{count} } @users;
    I favor the addition of :: to ensure that $a and $b remain global. This would have made a difference if the comparison was performed using the greater operator, >, and $a was lexical. For example, this will not work:
    my $a = 'john'; @users = sort { $a->{count} > $b->{count} } @users;

      users is a hash, and we know this because of the brackets following $users: $users{$value}->{count}, as demonstrated in the original question.

      Based on what the OP told us, the structure could minimally look something like this:

      %users = ( john => { count => 1 }, frank => { count => 3 } );

      If the goal is to sort the list of users based on the value of count, you're not really doing that. You're jumping to the assumption that the datastructure looks more like this:

      @users = [ { count => 1 }, { count => 3 } ];

      In other words, your solution assumes that @users is an array of hashrefs, as opposed to being a hash at its top level. I don't see how you're deriving that assumption based on the original question.

      What your solution accomplishes is to sort the array, @users, based on $users->{count}, not based on $users{$value}->{count}.

      Another perplexing topic covered in your reply is that of using '>' as the comparison operator within a sort routine. sort expects a comparison that returns either -1, 0, or 1. cmp does this, as does <=>. But there is nothing that will cause the '>' (greater than) operator to return a -1. So you're correct that "this won't work", but it has nothing to do with your choice of $::a and $::b versus $a and $b, and everything to do with an inappropriate use of the '>' operator. sort always uses a package global version of $a and $b anyway.


      Dave

      A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Sorting data structure
by tphyahoo (Vicar) on Oct 14, 2005 at 09:51 UTC
    I did it with an Hoh. Not sure if it's the best way... just what my instinct popped out...
    use strict; use warnings; my $users = {}; $users->{fred}->{count}=1; $users->{sarah}->{count}=2; $users->{arthur}->{count}=1; $users->{benjamin}->{count}=10; $users->{phil}->{count}=3; $users->{betty}->{count}=4; $users->{ilsa}->{count}=5; print $_ . "\n" for sort { $users->{$a}->{count} <=> $users->{$b}->{co +unt} } keys %$users;

    UPDATE: Or, you could use a hash, like the op did:

    use strict; use warnings; my %users; $users{fred}->{count}=1; $users{sarah}->{count}=2; $users{arthur}->{count}=1; $users{benjamin}->{count}=10; $users{phil}->{count}=3; $users{betty}->{count}=4; $users{ilsa}->{count}=5; print $_ . "\n" for sort { $users{$a}->{count} <=> $users{$b}->{count} + } keys %users;
    For me the HoH seems more "readable" but I couldn't say why. timtowtdi...
Re: Sorting data structure
by mhearse (Chaplain) on Oct 14, 2005 at 14:57 UTC
    Thanks all for the replies. Using the solution given, I'm trying to iterate over the values like so:
    for my $user ((sort { $users{$b}->{count} <=> $users{$a}->{count} } ke +ys %users) { blah blah... }
    But I get compile time errors
    Global symbol "$user" requires explicit package name at
    Is the problem obvious? Right now I'm being forced to sort to a list, then iterated over the items in the list:
    my @sorted_users = sort { $users{$b}->{count} <=> $users{$a}->{count} } keys %users; for (@sorted_users) { blah... blahh }

      Does the symbol $user appear somewhere outside of the loop? Its scope is constrained to inside the loop. If you try to access $user outside the loop you'll get a fatal error under strictures.

      Also check to ensure that the line preceeding your for loop isn't missing a semicolon at the end of the line. ;)


      Dave

        Here is the entire loop:
        for my $location (sort {$a cmp $b} keys %user_locations) { $worksheet->write($row++, $col, "$location ($user_locations{$loca +tion})", $format); my @sorted_users = sort { $users{$b}->{count} <=> $users{$a}->{count} } keys %users; for my $user (for my $user ((sort { $users{$b}->{count} <=> $users +{$a}->{count} } keys %users) { next unless $users{$user}->{suffix} eq $user_locations{$locati +on}; for (my $tmp=1; $tmp <= keys %sheet_layout; $tmp++) { $worksheet->write($row, $col++, $users{$user}->{$sheet_lay +out{$tmp}->[0]}, $format_r); } $col = 0; $row++; } $row++; }
        I can't find the problem right now, maybe I should stare at it a while longer...

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (5)
As of 2024-04-26 07:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found