in reply to Sorting an hash array
Two things.
Remember, you cannot sort a hash as such. You can take its keys and sort them as a list, but the hash itself does not have any specific order to it; there is no concept of "the first key", "the second key" and so on. All the keys are created equal, as it were; none takes precedence over any other.
(Side note: of course functions such as keys, values or each will iterate through a hash in a certain order, by necessity, but this order is essentially random, and code that relies on any specific order there is broken, even if the order happens to be reproducible.)
Anyhow, getting back to your question, try this:
use feature qw/say/; ... my $info = "out.txt"; open my $out, ">", $info or die "Cannot open file for writing: $!"; say $out "Key: $_ and Value: $functions{$_}" foreach (sort { $function +s{$a} cmp $functions{$b} } keys %functions); close $out or die "Cannot close file: $!";
I've also taken the liberty of making a other few changes to your code, namely:
They're not the law, but they're good ideas anyway. ;)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Sorting an hash array
by GrandFather (Saint) on Jul 26, 2014 at 01:59 UTC | |
|
Re^2: Sorting an hash array
by kepler (Scribe) on Jul 25, 2014 at 23:32 UTC | |
by AnomalousMonk (Archbishop) on Jul 26, 2014 at 00:26 UTC | |
by BillKSmith (Monsignor) on Jul 26, 2014 at 04:31 UTC |