Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: Hash Question

by lindex (Friar)
on Oct 06, 2000 at 05:48 UTC ( [id://35519]=note: print w/replies, xml ) Need Help??


in reply to Hash Question

I think this might work for you, basicly all it does is takes two hashes with the same keys but different values and then merges them into one hash with the same keys but values as arrayref's of the values of the other two hashes :)
use strict; my(%hash1) = ( 'foo' => 'bar', 'bleh' => 'qwer', 'lala' => 'asdf' ); my(%hash2) = ( 'foo' => 'bvcx', 'bleh' => 'lkjh', 'lala' => 'iopj' ); my(%hash3) = map { $_ => [$hash1{$_}] } keys(%hash1); map { push(@{$hash3{$_}},$hash2{$_}); } keys(%hash2); foreach (keys(%hash3)) { print "$_ is\n"; map { print "\t$_\n"; } @{$hash3{$_}}; }



lindex
/****************************/ jason@gost.net, wh@ckz.org http://jason.gost.net /*****************************/

Replies are listed 'Best First'.
RE: Re: Hash Question
by chromatic (Archbishop) on Oct 06, 2000 at 08:28 UTC
    This could be made into one line, but it's too late at night to do the formatting change:
    foreach my $key (keys %hash1) { $hash1{$key} = [ defined $hash2{$key} ? ($hash1{$key}, $hash2{$key}) : $hash1{$key} ]; }
    That could be made to go into a different hash, if that's your thing.
      better yet :) (at least I think this will work)
      $hash3 = map { $_ => [ $hash1{$_}, $hash2{$_} ] } keys(%hash1);
      p.s. vroom should allow us to customize the size of our text fields... I work on a 20 inch monitor with I.E in fullscreen mode :)


      lindex
      /****************************/ jason@gost.net, wh@ckz.org http://jason.gost.net /*****************************/

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (5)
As of 2024-04-19 17:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found