Two things.
- <=> is for numerical comparison; you want the cmp operator instead. See Equality Operators.
- Although you're sorting your hash keys, you're not actually doing anything with them, and when you're outputting to your file, you're going through them in their natural (read: random) order again.
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:
- Using lexical filehandles.
- Using the three-argument form of open.
- Error-checking.
- Using say when you want a trailing newline.
They're not the law, but they're good ideas anyway. ;)
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.