Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: how do I sort on two fields in a hash table?

by a (Friar)
on Mar 16, 2001 at 11:32 UTC ( [id://64894]=note: print w/replies, xml ) Need Help??


in reply to how do I sort on two fields in a hash table?

Another route is to rethink your data structure; if you have an array of hashs w/ each hash having 2 sort/index fields, try a hash (w/ count as the key) where the values are arrays of hashs which have type, a_point, z_point, error as members, something like:
foreach $count ( sort keys %data ){ # there's a better notation for this but $data{$count}->@ # never works for my foreachs ;-> foreach $type_hash ( @{ $data{$count} } ) { print "$count"; # just to make it easier to see, loop through the keys # for the type hash in your order. NB type_hash is # hash ref ($$type_hash) foreach $val ( (qw(type a_point z_point error) ) ) { print " --> $$type_hash{$val}"; } # foreach val print "\n"; } # foreach type_hash } # foreach count
This may be way off, for your data - the thought is that if you're having to work this hard to sort your data structure, you should think about changing your struct. Assuming you have data like:
count|type|a point|z point|error
3    |bz  |6.0    |7.2    |none
3    |boz |2.0    |1.1    |off by one
5    |bz  |3.5    |2.2    |ugly
you could:
while(<DATA>) { chomp; my ($count, $type, $a_point, $z_point, $error) = split(/\|/); push @{ $data{$count} }, {type => $type, a_point => $a_point, z_point => $z_point, error => "$error", }; } # while DATA

a

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (6)
As of 2024-04-20 00:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found