Ahh, what this really boils down to is a code problem (sulfericacid gave me a hint after i inquired about that curious while loop).

If have you a hash like so:

my %word = ( 'list' => 3, 'thats' => 1, 'value' => 2, 'an' => 1, 'would' => 1, 'the' => 3, 'at' => 1, 'if' => 2, );
And you want to sort it by the values, then you shouldn't use a while loop with each. Instead, use a for loop with sort and keys.

First example - i want to print the above hash sorted by the keys in ascending order:

print "$_ => $word{$_}\n" for sort keys %word;
Second example - sort by the values in ascending order:
print "$_ => $word{$_}\n" for sort {$word{$a} <=> $word{$b}} keys %word;
What's going on with $a and $b? Those are special built-in variables used for sorting. You should read the docs on sort. Moving on:

Third example - sort first by the values in descending order this time, and then sort by the keys in ascending order:

print "$_ => $word{$_}\n" for sort { $word{$b} <=> $word{$a} # sort values desc || # if same, then $a cmp $b # sort keys asc } keys %word ;
Finally, here is a complete script for your amusement. It reads in some text from the DATA filehandle (for convenience) and tries to filter out "words". These "words" are stored into a hash, and the 'histogram' is printed out and the end. The mystery of $blank is up to you to solve. ;)
use strict; use warnings; my %word; while (<DATA>) { $word{"\L$_"}++ for map {s/\W//g;$_} split; } my $blank = delete $word{''}; printf("% 14s => %d\n",$_,$word{$_}) for sort { $word{$b} <=> $word{$a} # sort values desc || # if same, then $a cmp $b # sort keys asc } keys %word ; print "$blank 'errors' encountered\n"; __DATA__ I'm having a little trouble following the documentation on pushing. It gives us the example: for $value (LIST) { $ARRAY[++$#ARRAY] = $value; } I'm assuming (LIST) should be an array list, and if that's so it would have been nice if they said that in the docs because at first I thought it was a filehandle of some kind.

jeffa

L-LL-L--L-LL-L--L-LL-L--
-R--R-RR-R--R-RR-R--R-RR
B--B--B--B--B--B--B--B--
H---H---H---H---H---H---
(the triplet paradiddle with high-hat)

In reply to (jeffa) Re: array pushing by jeffa
in thread array pushing by sulfericacid

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.