Here's a method I use. It may not be the most efficient or elegant, but it works.
Basically, I use a hash array to get a union of the array and the hash (it could just as easily be 2 hashes), while keeping track of where the data came from....
Note that I stuck in some dummy data....
use strict;
my @user = (1,3,5,7,9);
my %log = ( 1 => 'today',
2 => 'today',
3 => 'yesterday',
4 => 'last week',
5 => 'now' );
my %result;
foreach my $x (@user) {
# Save the id ($x) in the result hash, save the location
# of data as value, 'both' if $log{$x} exists, user if not
$result{$x} = $log{$x} ? 'both' : 'user';
}
foreach my $y (keys %log) {
# Save the id ($y) in the result hash, save the location
# of data as value only if the id does not exist in result
# hash yet
$result{$y} = 'log' unless $result{$y};
}
foreach my $z (sort keys %result) {
print "$z: $result{$z}\n";
}
Here are the results:
1: both
2: log
3: both
4: log
5: both
7: user
9: user
Hope this helps.
Brian |