Hi Owen,

On Tuesday 11 May 2010 14:10:49 Owen wrote:
> I have this statement;
> 
> foreach my $key ( keys %filehash ) {
>     print "$key $filehash{$key}->[0]\t\t\t$filehash{$key}->[3]\t
> $filehash{$key}->[2]\t $filehash{$key}->[1]\n";
> }
> 
> but wish to sort the output by $filehash{$key}->[0]. Is it possible? How
> do I do this?

Use http://perldoc.perl.org/functions/sort.html (perldoc -f sort). Untested:

[code]
foreach my $key ( 
        sort { $filehash{$a}->[0] cmp $filehash{$b}->[0] } 
        keys(%filehash)
)
{
# Do stuff with $key.
.
}
[/code]

Replace "cmp" with "<=>" if you want a numerical sort instead of a string-wise 
(= lexicographical) sort.

perldoc -f sort accepts any arbitrary expression for the comparison. Normally 
it should be symmetric between EXPR($a) COMPARE EXPR($b). Also see the "||" 
operator for chaining comparisons (e.g: << ($a->[0] cmp $b->[0]) || ($a->[1] 
<=> $b->[1]) >>).

Regards,

        Shlomi Fish
> 
> I can't make a separate hash of the value because of multiple values of
> the value
> 
> http://perldoc.perl.org/perlfaq4.html#How-do-I-sort-a-hash-(optionally-by-v
> alue-instead-of-key)%3f ( http://goo.gl/Ol31 )
> only shows the sorting where the hash has only a scalar.
> 
> 
> 
> TIA
> 
> 
> Owen

-- 
-----------------------------------------------------------------
Shlomi Fish       http://www.shlomifish.org/
Interview with Ben Collins-Sussman - http://shlom.in/sussman

God considered inflicting XSLT as the tenth plague of Egypt, but then
decided against it because he thought it would be too evil.

Please reply to list if it's a mailing list post - http://shlom.in/reply .

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to