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

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: how do i sort hash values?
by cdarke (Prior) on Oct 09, 2006 at 11:03 UTC
    Not easy to figure out exactly what you need, so apologies if I am incorrect.
    First, I am uncertain why you require a hash, after all you cannot sort a hash, and it has no order.
    The easiest way to get a unique number for each character is to use ord().
    my @array = split '', $string; @hash{@array} = map {ord} @array;

    If you then want to list the key/value pairs, use a 'for' loop with reverse() and sort(). As stated above, it would be best if you looked these up yourself, but to get you started:
    for my $key (reverse sort keys %hash) { print "$key: $hash{$key}\n" }
      hi, thanks ya, its working.
Re: how do i sort hash values?
by johngg (Canon) on Oct 09, 2006 at 11:42 UTC
    It is difficult to divine from your question but my guess is that you want to tag each letter in a word with it's rank in reverse alphabetic order. If that is the case your "bioinfo" example should actually be "5313241" since "f" comes before "i" and "n". This script works by building a hash table of letters in the word with their rank in reverse alpha order.

    use strict; use warnings; my @words = qw{bioinfo alpha different}; foreach my $word (@words) { print qq{$word\n}; my $rhRank = rank($word); print qq{$_ -- $rhRank->{$_}\n} for split m{}, $word; } print qq{\n}; sub rank { my $word = shift; my %seen = (); my $index = 0; my $rhRank = { map {$_, ++ $index} sort {$b cmp $a} grep {! $seen{$_} ++} split m{}, $word }; }

    When run for three words it produces

    bioinfo b -- 5 i -- 3 o -- 1 i -- 3 n -- 2 f -- 4 o -- 1 alpha a -- 4 l -- 2 p -- 1 h -- 3 a -- 4 different d -- 7 i -- 4 f -- 5 f -- 5 e -- 6 r -- 2 e -- 6 n -- 3 t -- 1

    I hope I have guessed correctly and this is of use.

    Cheers,

    JohnGG

Re: how do i sort hash values?
by prasadbabu (Prior) on Oct 09, 2006 at 10:28 UTC
    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: how do i sort hash values?
by shmem (Chancellor) on Oct 09, 2006 at 11:26 UTC
    You can acces hash values only via the keys, so you must sort them, comparing the values to have the values nicely lined up:
    my %hash = (f => 'd', c => 'a', b => 'e'); my @keys_ordered_by_value = sort { $hash{$a} cmp $hash{$b} } keys %has +h; # ^^^ # \|/ # to sort numeric values use <=> here -------+ # to sort in reverse order swap $a and $b: { $hash{$a} cmp $hash{$b} + } foreach my $key (@keys_ordered_by_value) { print "key = $key, value = $hash{$key}\n"; }
    This is a FAQ. See perlfaq4.

    --shmem

    _($_=" "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}
Re: how do i sort hash values?
by planetscape (Chancellor) on Oct 09, 2006 at 19:21 UTC