Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

sort a hash with split values

by Anonymous Monk
on Jun 21, 2006 at 17:39 UTC ( [id://556737]=perlquestion: print w/replies, xml ) Need Help??

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

I know this has been asked but I'm not sure what to search. I have a hash of users with their data (age, location, etc) with delimiters.
$hash{$username} = "12:NY:me@yoiu.com";
I need to sort only on the SECOND delimited thing. So in this example, I need to sort the hash by the state the user is from.

I will never need to sort by any other fields but this one. Can someone give me pointers?

Replies are listed 'Best First'.
Re: sort a hash with split values
by shmem (Chancellor) on Jun 21, 2006 at 17:45 UTC
    You may want to look at Schwartzian Transform:
    my @sorted_keys = map { $_->[0] } sort { $a->[1] cmp $b->[1] } map { [ $_, (split/:/,$hash{$_})[1] ] } keys $hash;
    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
      Hi. that worked great! Now I don't understand all that code so I'm playing with it. I want to now sort on the THIRD delimited data. I changed all of the 1s in there to 2s and it doesn't appear to be working. Need I change something else?
      A reply falls below the community's threshold of quality. You may see it by logging in.

      Never been a big fan of ST ... it's kinda like a one night stand ... sure it feels good but you feel ashamed afterwards (or so I'm told). Just use temp variables ...

      #!/usr/bin/perl use strict; use warnings; my %hash = ( 'foo00' => '12:NY:me@yoi00.com', 'foo01' => '12:NJ:me@yoi01.com', 'foo02' => '12:NV:me@yoi02.com', 'foo03' => '12:AL:me@yoi03.com', 'foo04' => '12:AK:me@yoi04.com', 'foo05' => '12:MD:me@yoi05.com', 'foo06' => '12:MO:me@yoi06.com', 'foo07' => '12:MI:me@yoi07.com', 'foo08' => '12:MA:me@yoi08.com', 'foo09' => '12:CA:me@yoi09.com', ); # create array that looks like this # [ # [ foo00, NY ], # [ foo01, NJ ], # .. # [ foo09, CA ] # ] my @temp; foreach my $key ( keys %hash ) { my @values = split( /:/, $hash{$key} ); push( @temp, [ $key, $values[1] ] ); } # sort array by second element my @sort = sort { $a->[1] cmp $b->[1] } @temp; # roll through sort and use # first element of array as key into hash foreach my $ref ( @sort ) { print "$ref->[0]: $hash{$ref->[0]}\n"; }
      So ST is compact but too dense for me (or maybe I'm too dense for ST).

      -derby
Re: sort a hash with split values
by salva (Canon) on Jun 21, 2006 at 20:40 UTC
    use Sort::Key qw(keysort); @sorted = keysort { (split /:/, $hash{$_})[1] } keys %hash;
Re: sort a hash with split values
by TedPride (Priest) on Jun 22, 2006 at 04:00 UTC
    Well, you could just dump the usernames into state buckets and then retrieve them by state:
    use strict; use warnings; my (%hash, %states); while (<DATA>) { chomp; @_ = split /: /, $_, 2; $hash{$_[0]} = $_[1]; } for (keys %hash) { push @{$states{(split /:/, $hash{$_})[1]}}, $_; } for (sort keys %states) { print $hash{$_}, "\n" for @{$states{$_}}; } __DATA__ alpha: 12:NY:me@yoiu.com beta: 13:MO:fg@yoiu.com gamma: 14:DE:fghh@yoiu.com delta: 15:MO:adf@yoiu.com epsilon: 16:DE:mjhg@yoiu.com iota: 17:NY:juyu@yoiu.com eta: 18:NY:mjhk@yoiu.com
    The transform method is so much neater, though.
Re: sort a hash with split values
by Fletch (Bishop) on Jun 21, 2006 at 17:43 UTC

    perldoc -q sort

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (3)
As of 2024-04-16 13:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found