Akens, Anthony wrote:
> Hi,
> I'm new to hashes, and I've been playing around with the
> following for a while... I'm just not getting it.
> 
> I have two hashes, one containing data read in from a file,
> one with "current" data.  I'd like to merge the two, adding
> any new keys and values into the hash, and for any keys that
> exist in both, I'd like the append the values onto the values
> for the same key in the original hash.
> 
> An example...
> 
> Hash 1 (from a file)
> 
> host1=>"760,760,759"
> host2=>"765,760,760"
> host5=>"130,200"
> 
> Hash 2 (Current)
> 
> host1=>"758"
> host2=>"760"
> host4=>"450"
> host5=>"210"
> 
> I'd like the merged hash to be:
> 
> host1=>"760,760,759,758"
> host2=>"765,760,760,760"
> host5=>"130,200,210"
> host5=>"450"
> 
> 
> I'm using an example out of the perl cookbook, and trying to
> modify it to do what I want, but it isn't working out...
> 
> The three hashes are:
> %host_list   (Hash 1 in the example)
> %current_list   (Hash 2 in the example)
> %final_list  (The result of the merge)
> 
> Here's the code I have so far for the merge (this is borrowed
> heavily from the Perl Cookbook):
> 
> foreach $item ( \%host_list, \%current_list) {
>     while (($key, $value) = each %$item) {
>         if (exists $final_list{$key}) {
>           $value .= ",$current_list{$key}";
>         }
>         $final_list{$key} = $value;
> 
>     }
> }
> 
> 
> Can someone point me in the right direction?
> 
> - Tony
        Tony, I removed the final list and just used the current list to build a 
combined list into current_list. 
        It is a start.
Wags ;) output of hte processing comes after script from Data::Dumper.

=====================================
#!perl -w

use strict;
use Data::Dumper;

my %host_list     = (   host1=>"758",
                        host2=>"760",
                        host4=>"450",
                        host5=>"210"
                  );
my %current_list  = (   host1=>"760,760,759",
                        host2=>"765,760,760",
                        host5=>"130,200"
                  );

print Dumper( \%host_list);
print Dumper( \%current_list);
=hea1
Here's the code I have so far for the merge (this is borrowed
heavily from the Perl Cookbook):
=cut

foreach my $item ( keys %host_list ) {
    $current_list{$item} .= ',' if ( exists $current_list{$item} );
    $current_list{$item} .= $host_list{$item};  
}

print Dumper( \%host_list);
print Dumper( \%current_list);

Output of processing:
$VAR1 = {
          'host4' => '450',
          'host5' => '210',
          'host1' => '758',
          'host2' => '760'
        };
$VAR1 = {
          'host5' => '130,200',
          'host1' => '760,760,759',
          'host2' => '765,760,760'
        };
$VAR1 = {
          'host4' => '450',
          'host5' => '210',
          'host1' => '758',
          'host2' => '760'
        };
$VAR1 = {
          'host4' => '450',
          'host5' => '130,200,210',
          'host1' => '760,760,759,758',
          'host2' => '765,760,760,760'
        };


**********************************************************
This message contains information that is confidential
and proprietary to FedEx Freight or its affiliates.
It is intended only for the recipient named and for
the express purpose(s) described therein.
Any other use is prohibited.
****************************************************************


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to