in reply to array pushing

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)

Replies are listed 'Best First'.
Re: (jeffa) Re: array pushing
by sulfericacid (Deacon) on Apr 26, 2003 at 16:56 UTC
    Thanks for your help everyone, I greatly appreciate it. It turns out that since I was using a hash to begin with I didn't have to turn it into an array in order to sort it the way I needed. Not sure why I had the crazy idea it had to be an array but the problem was quickly solved when jeffa pointed out I could just sort the hash I was working with.

    When the time comes I'll be prepared to use arrays like this, thanks everyone.

    "Age is nothing more than an inaccurate number bestowed upon us at birth as just another means for others to judge and classify us"

    sulfericacid

      Ahh, but you are using an array to sort with! Consider these:
      print $key for $key sort keys %hash; print $val for $val sort values %hash;
      Sure, the far right side is a hash, but what are keys and values? They are built-in functions (just like sort) that return lists. The trick is what you do with those lists.

      What is the difference between a list and array? Well, you did take The Mad Hatter's advice and read japhy's article, didn't you? Just remember, a lot of folks (including myself) have a hard time understanding that material. Sometimes the concept itself is just too darned difficult for anyone to teach it in an easy to digest manner ... repeated reading / experimentation is how i learn that stuff. Sometimes that revelation doesn't hit you until years have passed. Keep on truckin' ... or change your interest and do something else. ;)

      jeffa

      rinse ... repeat ...