Problems? Is your data what you think it is? 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{\$_});
}

Replies are listed 'Best First'.
Re^2: Numeric sorting WITHOUT <=>
by Marshall (Abbot) 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" );

Create A New User
Node Status?
node history
Node Type: note [id://998113]
help
Chatterbox?
 [Lady_Aleena]: I'll just have to search for the files using the subs and open them up. [Discipulus]: nice Perl dreams, folks.. shower->Dumas->bed [erix]: pere ou fils? [Lady_Aleena]: Discipulus, good night. [Discipulus]: pere, le vicomte de bragelonne.. wonderfull [erix]: I'll keep that in mind (haven't read), buona notte :) [Discipulus]: oh erix but is the fourth of cycle;read them in order [erix]: hm, perhaps I can just read it here Discipulus zzz...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (8)
As of 2017-05-23 19:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
My favorite model of computation is ...

Results (182 votes). Check out past polls.