Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: sorting hashes

by davido (Cardinal)
on Mar 24, 2009 at 20:21 UTC ( [id://752960]=note: print w/replies, xml ) Need Help??


in reply to sorting hashes

I started working up an example solution and quickly realized that I'm unclear on your specification. The question I have is this: Does your hash look like this:

my %hash; @hash{ qw/number score alignment/ } = @cols;

Or does it look like this:

my %hash; @hash{ qw/number score alignment/ } = map{ \$cols[$_] } 0 .. $#cols;

Or does it look like this:

my %hash = ( number => '$cols[0]', scorecons => '$cols[1]' alignment => '$cols[2]' );

In other words, is $cols[0] to be represented in your hash as the value of $cols[0], as a reference to the element (ie, \$cols[0]), or as the literal string '$cols[0]? The last option is unlikely, but the first two are not unreasonable interpretations of your question, and yet they change the look of any example solution.

Please clarify a little for us. Until we have that ironed out we can't really show you how the solution might look.

Also remember that Perl's hashes are unsorted by definition. If you need to present the contents of a hash in some sorted order, you can do that on the fly, but keep in mind that if you refer back to that sorted order frequently you might find it convenient to maintain a separate sorted list; a parallel list, so to speak. In fact, your @cols array could even be an AoA that keeps track of keys and values in a sorted order such as:

@cols = ( [ qw/value1 number / ], [ qw/value2 scorecons/ ], [ qw/value3 alignment/ ] );

You could still maintain the hash too for quick lookups.


Dave

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (6)
As of 2024-04-23 09:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found