Professional Web Applications Themes

adding hash to a hash - PERL Beginners

Howdy group, This seems like it should be simple, but I'm a bit stumped on the best way to do this: I want to add a hash to a hash, With arrays you could: push arr2, arr2; but what is the best way to do that with a hash? perl -MData::Dumper -mstrict -we 'my %q=(1=>2,3=>4);my %w=(5=>6,7=>8);print Dumper \%q;' So how can I add %w to %q in that example? IE so it outputs: $VAR1 = { '1' => 2, '7' => 8, '5' => 6, '3' => 4 }; TIA Lee.M - JupiterHost.Net...

  1. #1

    Default adding hash to a hash

    Howdy group,

    This seems like it should be simple, but I'm a bit stumped on the best
    way to do this:

    I want to add a hash to a hash,
    With arrays you could:
    push arr2, arr2;

    but what is the best way to do that with a hash?

    perl -MData::Dumper -mstrict -we 'my %q=(1=>2,3=>4);my
    %w=(5=>6,7=>8);print Dumper \%q;'

    So how can I add %w to %q in that example?

    IE so it outputs:

    $VAR1 = {
    '1' => 2,
    '7' => 8,
    '5' => 6,
    '3' => 4
    };

    TIA

    Lee.M - JupiterHost.Net
    JupiterHost.Net Guest

  2. #2

    Default Re: adding hash to a hash


    ----- Original Message -----
    From: "JupiterHost.Net" <net>
    To: <org>
    Sent: Sunday, September 19, 2004 6:56 PM
    Subject: adding hash to a hash

     

    Wouldn't something like

    my(%a)=(a1=>1,a2=>2);
    my(%b)=(b1=>1,b2=>2);
    my(%c)=huh(%a,%b);

    ....

    sub huh {
    return _;
    };

    work?
    Felix Guest

  3. #3

    Default Re: adding hash to a hash

    On Sep 19, JupiterHost.Net said:
     

    The general way is:

    # to add %w to %q
    q{keys %w} = values %w;

    If there are overlapping keys, %w's values will be used.

    --
    Jeff "japhy" Pinyan % How can we ever be the sold short or
    RPI Acacia Brother #734 % the cheated, we who for every service
    http://japhy.perlmonk.org/ % have long ago been overpaid?
    http://www.perlmonks.org/ % -- Meister Eckhart

    Jeff Guest

  4. #4

    Default Re: adding hash to a hash

    Jeff 'japhy' Pinyan wrote: 
    >
    > The general way is:
    >
    > # to add %w to %q
    > q{keys %w} = values %w;
    >
    > If there are overlapping keys, %w's values will be used.[/ref]

    Why not just

    %q = (%q, %w);

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Guest

  5. #5

    Default Re: adding hash to a hash

    On Sep 20, Gunnar Hjalmarsson said:
     
    >>
    >> The general way is:
    >>
    >> # to add %w to %q
    >> q{keys %w} = values %w;
    >>
    >> If there are overlapping keys, %w's values will be used.[/ref]
    >
    >Why not just
    >
    > %q = (%q, %w);[/ref]

    Benchmark it to see which is better. Using %q = (%w, %q) allows you to
    keep %q's values, though.

    --
    Jeff "japhy" Pinyan % How can we ever be the sold short or
    RPI Acacia Brother #734 % the cheated, we who for every service
    http://japhy.perlmonk.org/ % have long ago been overpaid?
    http://www.perlmonks.org/ % -- Meister Eckhart

    Jeff Guest

  6. #6

    Default Re: adding hash to a hash

    Jeff 'japhy' Pinyan wrote: 
    >>
    >> Why not just
    >>
    >> %q = (%q, %w);[/ref]
    >
    > Benchmark it to see which is better.[/ref]

    The slice method seems to be faster.

    use Benchmark 'cmpthese';
    my %y = (three => 3, four => 4);
    cmpthese -5, {
    hashslice => sub {
    my %x = (one => 1, two => 2);
    x{ keys %y } = values %y;
    },
    lists => sub {
    my %x = (one => 1, two => 2);
    %x = (%x, %y);
    },
    };

    Result:
    Rate lists hashslice
    lists 21936/s -- -40%
    hashslice 36517/s 66% --
     

    And requires less typing. :)

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Guest

  7. #7

    Default Re: adding hash to a hash


    Thanks for the input! Bit of a brain freeze there ;p

    All 3 methods have the desired effect.

    Here is the benchmark info for thsoe playing along:

    Benchmark: timing 1000000 iterations of %q = (%q, %w); , q{keys %w} =
    values %w;, my(%c)=huh(%a,%b);...

    %q = (%q, %w); : 0.459673 wallclock secs ( 0.46 usr + 0.00 sys = 0.46
    CPU) 2173913.04/s (n=1000000)

    q{keys %w} = values %w;: 0.574297 wallclock secs ( 0.63 usr + 0.00 sys
    = 0.63 CPU) 1587301.59/s (n=1000000)

    my(%c)=huh(%a,%b);: 0.672142 wallclock secs ( 0.60 usr + 0.00 sys =
    0.60 CPU) 1666666.67/s (n=1000000)

    Rate q{keys %w} = values %w;
    my(%c)=huh(%a,%b); %q = (%q, %w);

    q{keys %w} = values %w; 1587302/s --
    -5% -27%

    my(%c)=huh(%a,%b); 1666667/s 5%
    -- -23%

    %q = (%q, %w); 2173913/s 37%
    30% --

    I hope the mail client doesn't butcher it too much :)

    Lee.M - JupiterHost.Net
    JupiterHost.Net Guest

  8. #8

    Default Re: adding hash to a hash

    Ok,

    I think I've decided to go with 'assign'

    1) Because the benchmark difference isn't much:

    splice 2008032/s -- -3%
    assign 2074689/s 3% --

    2) And duplicate keys become the value of the newest hash on both ways:


    use Data::Dumper;
    my %w = (1=>2,3=>4);
    my %q = (3=>6,7=>8);

    q{keys %w} = values %w;
    print Dumper \%q;

    %q = (3=>6,7=>8);
    %q = (%q, %w);
    print Dumper \%q;


    $VAR1 = {
    '1' => 2,
    '3' => 4,
    '7' => 8
    };
    $VAR1 = {
    '1' => 2,
    '3' => 4,
    '7' => 8
    };

    3) It is less typing :)

    Thanks again for all the ideas!

    Lee.M - JupiterHost.Net
    JupiterHost.Net Guest

Similar Threads

  1. hash of hash of array slices
    By Paul Kraus in forum PERL Beginners
    Replies: 6
    Last Post: January 26th, 05:55 PM
  2. adding an array as a hash value
    By Rob Dixon in forum PERL Beginners
    Replies: 0
    Last Post: October 14th, 02:58 PM
  3. Sort a hash based on values in the hash stored as arrays of hashes
    By Tore Aursand in forum PERL Miscellaneous
    Replies: 3
    Last Post: September 16th, 10:14 AM
  4. Another reference question (hash of hash references)
    By Tim Musson in forum PERL Beginners
    Replies: 2
    Last Post: August 8th, 01:11 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139