Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: Numeric sorting WITHOUT <=>

by ikegami (Pope)
on Oct 10, 2012 at 01:55 UTC ( #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{$_}); }


Comment on Re: Numeric sorting WITHOUT <=>
Download Code
Re^2: Numeric sorting WITHOUT <=>
by Marshall (Prior) 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
Node Status?
node history
Node Type: note [id://998113]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (2)
As of 2014-09-22 04:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (178 votes), past polls