Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: Numeric sorting WITHOUT <=>

by ikegami (Patriarch)
on Oct 10, 2012 at 01:55 UTC ( [id://998113]=note: print w/replies, xml ) Need Help??


in reply to Numeric sorting WITHOUT <=>

my ($min) = keys(%jetsons); my $max = $min; for (keys(%jetsons)) { $max = $_ if $_ > $max; $min = $_ if $_ < $min; } my @sorted; for ($min..$max) { push @sorted, $_ if exists($jetsons{$_}); }

Replies are listed 'Best First'.
Re^2: Numeric sorting WITHOUT <=>
by Marshall (Canon) on Oct 10, 2012 at 04:42 UTC
    my ($min) = keys(%jetsons); This is the number of keys in %jetsons.
    This is not the min.
      my ($min) = keys(%jetsons); This is the number of keys in %jetsons.

      Actually, it's a randomly (insofar as the hashing algorithm is random) chosen key from the hash. The assignment is in list context and seeds the min/max finder loop that follows.

      >perl -wMstrict -le "my %jetsons = qw(aa 1 bb 2 cc 3 dd 4 ee 5); my ($min) = keys %jetsons; print $min; " cc

      Update: ikegami's code works. (Never doubt a Pope.)

      No.
      # keys in scalar context: Number of keys. my $min = keys(%jetsons); # keys in list context: List of keys. First one is assigned my ($min) = keys(%jetsons);
        Correct.
        My mistake.
Re^2: Numeric sorting WITHOUT <=>
by ria1328 (Initiate) on Oct 10, 2012 at 17:07 UTC
    Thank you! This worked and it actually made it easier for me to understand how it was done instead of using the was my professor said we should!

      This worked and it actually made it easier for me to understand how it was done

      My code does NOT demonstrate how sorting is usually done. It takes a different and silly approach in response to your equally silly requirement. It would take forever for my solution to sort

      my %jetsons = ( 0 => "a", 4000000000 => "b" );

Log In?
Username:
Password:

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

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

    No recent polls found