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

Re: Cutting the top percentage out of a Hash

by ikegami (Patriarch)
on Apr 13, 2006 at 18:38 UTC ( [id://543178]=note: print w/replies, xml ) Need Help??


in reply to Cutting the top percentage out of a Hash

The hash is sorted in a specific order,

That's impossible. Hashes can't be sorted, as you've noticed. Are you sure an AoA isn't more appropriate here? If you wish to continue using a hash, the typical solution it the sort the data as you read it from the hash, as follows:

foreach my $key ( sort { ... } keys %lineFinal) { ... }

Alternatively, you could use Tie::IxHash instead of a hash. You use it just like a normal hash (thanks to tie), but the contents are ordered.

Replies are listed 'Best First'.
Re^2: Cutting the top percentage out of a Hash
by Limbic~Region (Chancellor) on Apr 13, 2006 at 18:49 UTC
    ikegami,
    Alternatively, you could use Tie::IxHash instead of a hash. The syntax is the same (thanks to tie), but the contents are ordered.

    It should be made clear that the order is the insertion order and not an arbitrary sort criteria. While the module does provide rudimentary sort capability, it is limited to asciibetical sorts of the keys or values.

    Tie::Hash::Sorted may be a better fit.

    Cheers - L~R

      Thanks for the info
      I was hoping to avoid using Tie:: Hash
      a) Because I haven't used it before and I'm not sure of syntax.
      b) I was hoping for a more simple solution that would avoid installing the Tie:: Hash module
      Gavin
        a) There's a first time for everything
        b) Installing a module sometimes is the simple solution, and we have CPAN conveniently provided for us

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (3)
As of 2024-04-25 22:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found