Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: Printing a hash in a specific order?

by artist (Parson)
on Mar 15, 2003 at 15:06 UTC ( #243294=note: print w/ replies, xml ) Need Help??


in reply to Printing a hash in a specific order?

Hi L~R,
you wrote:
Unfortunately, the object is stored in a hash - and we know that the hash typically comes out differently than the order it went in.

use Tie::IxHash - ordered associative arrays. Here is the sample:

# Prints this without tie: # apples # oranges # # Prints this with tie: # oranges # apples use Tie::IxHash; tie %menu, 'Tie::IxHash'; $menu{oranges} = 1; $menu{apples} = 2; foreach (keys %menu) { print "$_\n"; }
From Description of the module:
This Perl module implements Perl hashes that preserve the order in which the hash elements were added. The order is not affected when values corresponding to existing keys in the IxHash are changed. The elements can also be set to any arbitrary supplied order. The familiar perl array operations can also be performed on the IxHash.

artist


Comment on Re: Printing a hash in a specific order?
Download Code
Re: Re: Printing a hash in a specific order?
by Limbic~Region (Chancellor) on Mar 15, 2003 at 15:15 UTC
    artist,
    Thank you very much! I have to look under the hood of this module as I think I have two problems that may make using it a little less than straight forward.

  • The object may change over time so I would know how to order keys that haven't been created yet.
  • If it is truly forcing the order and not performing some other kind of magic, than it is going to hurt performance.

    In reference to point one, I could always just create a place holder for all possible keys, but I am not sure I want to do that since some parameters can exist with empty string as the value and I do not want to accidently create a parameter in a record that didn't previously exist. As far as point two. I have about 150,000 records in in the database - I wouldn't mind adding some overhead for ease of use as long as it doesn't add an extra hour to processing time.

    Thanks again - I will definately check out this module.
    Cheers - L~R

      Hi L~R,
      You can input your data in benchmarking here.

      My benchmark, comparing 200,00 records and each record contain a 50 fields containing small numeric data as key and value, gives the results:

      Benchmark: timing 200000 iterations of with_tie, without_tie...
        with_tie: 98 wallclock secs (96.22 usr +  0.00 sys = 96.22 CPU) @ 2078.61/s (n=200000)
      without_tie: 14 wallclock secs (13.25 usr +  0.00 sys = 13.25 CPU) @ 15094.34/s (n=200000)
                     Rate    with_tie without_tie
      with_tie     2079/s          --        -86%
      without_tie 15094/s        626%          --
      
      And the code is

      use Benchmark qw(cmpthese); use strict; use Tie::IxHash; sub with_tie { tie my %menu, 'Tie::IxHash'; foreach (1..50){$menu{$_} = $_; } } sub without_tie{ my %menu; foreach (1..50){$menu{$_} = $_;} } cmpthese(200000, { with_tie => \&with_tie, without_tie => \&without_tie });
      artist

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (6)
As of 2014-10-24 08:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (131 votes), past polls