Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re^3: Sorting integer value hash keys

by ikegami (Patriarch)
on Dec 25, 2009 at 19:25 UTC ( [id://814365]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Sorting integer value hash keys
in thread Sorting integer value hash keys

You're missing a newline on the last line.
say join "\n", sort { $a <=> $b } keys %{{ map { $_ => 0 } 1..10 }}
should becan be shortened to
say for sort { $a <=> $b } keys %{{ map { $_ => 0 } 1..10 }}
Furthermore, since you assume the list is constant, that can be simplified to
say for sort { $a <=> $b } 1..10;
And if you wanted to show that stringification doesn't matter, the following would suffice:
say for sort { $a <=> $b } map "$_", 1..10;

Replies are listed 'Best First'.
Re^4: Sorting integer value hash keys
by JadeNB (Chaplain) on Dec 25, 2009 at 19:41 UTC
    You're missing a newline on the last line.
    I think that you're mistaken—say (as opposed to print) provides it for me.
    Furthermore, since you assume the list is constant, that can be simplified to
    say for sort { $a <=> $b } 1..10;
    Yes, you're right; I contemplated coming back and updating the node with that, but then I would have had to point out that sort { $a <=> $b } 1..10 could be replaced by just 1..10, and that seemed to be going too far. :-)
    And if you wanted to show that stringification doesn't matter
    That wasn't my goal, largely because I'm not sure what you mean. UPDATE: Because I was too busy writing one-liners to read the OP's actual question; sorry.

      I think that you're mistaken—say (as opposed to print) provides it for me.

      Oops, right

      but then I would have had to point out that sort { $a <=> $b } 1..10 could be replaced by just 1..10,

      That wouldn't demo how to sort 10 after 9 rather than before 2.

      That wasn't my goal, largely because I'm not sure what you mean.

      The OP suspected that hashes keys are always strings and that stringification of the keys was the cause of his problem.

      Hash keys are always strings (except for magical hashes), but that's not the cause of his problem. Perl treats the number zero and the string zero identically (except for its bitwise operators).

      (I'm sure you know that. It's for the OP's benefit.)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://814365]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (6)
As of 2024-04-18 06:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found