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

Dear Monks I have written a program which extracts details from a text file (works fine)and stores in a hash, which prints out the results (works fine) but I am having difficulties sorting that hash so that the result is sorted into ascending numerical order. Why won't the sort work on my hash??; see code below.

foreach $word(@words){ $Counter = $Counter+1; #print("$word\n"); } $sentence_count{($Counter)} = $sentence_count{($Counter)}sort by_num ( +keys (%sentence_count)); while (($sentence_count,$word_count) = each(%sentence_count)) { print ("\nThere are $word_count sentences of $sentence_count words\n") +; } sub by_num { if ($b < $a) { return -1; } elsif ($a == $b){ return 0; } elsif ($b > $a){ return 1; } }

Replies are listed 'Best First'.
(jeffa) Re: hash sorting problems
by jeffa (Bishop) on Apr 20, 2001 at 22:06 UTC
    EEK!
    foreach $word(@words){ $Counter = $Counter+1; #print("$word\n"); }
    don't do that - if you want to know how many elements are in an array, just get it's value in SCALAR context:
    my $count = scalar @words; # style pointer: it is a $count not a $Counter
    If you want to know what the index of the last element is, you can subtract 1 from $count or better yet:
    # not very useful my $last = $#words; # very useful for(0..$#words) { ... }
    </code>

    Jeff

    R-R-R--R-R-R--R-R-R--R-R-R--R-R-R--
    L-L--L-L--L-L--L-L--L-L--L-L--L-L--
    
Re: hash sorting problems
by satchboost (Scribe) on Apr 20, 2001 at 21:42 UTC
    Ok..... First off, your sort should be something along the lines of sort {$a <=> $b} (keys %sentence_count). (This is known as the spaceship operator.)

    My main question, though, is where are you putting the result of the sort?? You have the line (as I read it):
    $sentence_count{($Counter)} = $sentence_count{($Counter)}sort by_num (keys (%sentence_count));

    This line doesn't make much sense to me. Update your node with the code reformatted, please.

      ok, look up perlfunc:sort and read about user defined sorting criteria.

      Have a nice day
      All decision is left to your taste
Re: hash sorting problems
by Sprad (Hermit) on Apr 20, 2001 at 21:54 UTC
    Is there some reason why you're rolling your own sort instead of using the built-in sort function? The Perl FAQ has a nice section on hashes, and there's even a question that gives sample code for sorting a hash.

    ---
    I'm too sexy for my .sig.

Re: hash sorting problems
by Beatnik (Parson) on Apr 21, 2001 at 02:40 UTC
    Hashes have both keys and values; they arent stored by indices (like arrays). Perl stores the hash elements internally by calcing some values... As already described above, try something similar to
    @sorted_keys = sort { $a <=> $b } keys %hash; #use { $a <=> $b } for numerical sorting #use { $a cmp $b } for string sorting foreach(@sorted_keys) { print $hash{$_}; }

    Greetz
    Beatnik
    ... Quidquid perl dictum sit, altum viditur.