Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re^2: sorting a complex multidimensional hash

by revdiablo (Prior)
on Jul 22, 2004 at 06:24 UTC ( [id://376478]=note: print w/replies, xml ) Need Help??


in reply to Re: sorting a complex multidimensional hash
in thread sorting a complex multidimensional hash

There's another option that falls somewhere in the middle or your two examples:

use warnings; use diagnostics; use strict; my @array = ("8.foo", "6.bar", "7.baz", "5.biz", "3.fizzle", "0.fro", "9.boz"); print join "\n", sort {by_number($a) <=> by_number($b)} @array; sub by_number { my $value = shift; if ($value =~ m/^(\d+)/) { return $1 } }

Now, of course this is going to be slower than the ST, but whether the optimization is necessary or not depends on a lot of things. Testing on my machine with an array of 70,000 elements, the ST takes 1.6 seconds and this one takes 5. That makes the ST 3.3x faster, but the increase might or might not be worth the more complex code. 5 seconds to sort 70,000 elements is not too shabby, and if the actual data is going to have far fewer, then the extra complexity just might not be worth it.

Replies are listed 'Best First'.
Re^3: sorting a complex multidimensional hash
by thor (Priest) on Jul 22, 2004 at 12:04 UTC
    That's fair. I suppose I've seen the "map-sort-map" construct enough times that I don't even blink at it. Heck, as long as we're on the subject, I may as well mention the only downfall (that I know) of the ST. As with a lot of things perl, you end up trading memory for speed. For every element in your initial list, you create an anonymous array of two elements. For large* lists, this can actually be prohibitive.

    thor


    For certain values of "large" :)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (2)
As of 2024-04-20 10:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found