> -----Original Message-----
> From: Craig Hammer [mailto:[EMAIL PROTECTED]]
> Sent: Wednesday, May 29, 2002 5:59 AM
> To: Perl (E-mail)
> Subject: converting a hash to an array
>
>
> I've found a couple of examples of moving an array into a
> hash, but not of
> moving a hash into an array.
>
> I thought this would work:
>
> @sorted = sort { $myhash{$b} <=> $myhash{a} } keys %myhash ;
^ s/b $a, I assume
So now @sorted is a list of the keys from %myhash, sorted by
the hash values (numeric sort).
>
> while ( <@sorted> ) {
> printf ( "first field = %s second field = %s\n",
> $sorted{1}, $sorted{2}
> ) ;
> }
I have no idea what this is trying to do.
>
>
> I am trying to sort the hash values (by value, not key) and
> move the sorted
> results into an array, so that I can add additional fields later.
What do you mean by "results"? The list of values? The list of
key/value pairs?
>
> I get an "Use of uninitialized value at ./newlogscan.pl line
> 46." error.
> Line 46 is the @sorted = line.
That's because you have $sorted{a} instead of $sorted{$a}. 'use strict'
will catch that.
>
> Any ideas?
If you want a sorted list of values:
my @list = sort { $a <=> $b } values %myhash;
If you want a list of key/value pairs, in order by the values:
my @list = map {($_, $myhash{$_})}
sort {$myhash{$a} <=> $myhash{$b}} keys %myhash
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]