#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
my @array1 = (1,2,3);
my @array2 = (1,2,3,4,5);
my %hash = map { $_ => 1 } @array1;
my %hash2 = map { $_ => 1 } @array2;
print "Before:\n" . Dumper(\%hash) . Dumper(\%hash2);
# remove the unwanted "value"
my $to_delete = 2;
my $old = delete($hash{$to_delete});
my $old2 = delete($hash2{$to_delete});
for my $x (keys %hash2) {
next if exists($hash{$x});
$hash{$x} = 1;
delete($hash2{$x});
last; # updated -- added this line
}
print "After:\n" . Dumper(\%hash) . Dumper(\%hash2);
__OUTPUT__
Before:
$VAR1 = {
'1' => 1,
'3' => 1,
'2' => 1
};
$VAR1 = {
'4' => 1,
'1' => 1,
'3' => 1,
'2' => 1,
'5' => 1
};
After:
$VAR1 = {
'4' => 1,
'1' => 1,
'3' => 1,
};
$VAR1 = {
'1' => 1,
'3' => 1,
'5' => 1,
};
Where do you want *them* to go today?
|