At 3:03 PM +0100 3/9/11, vito pascali wrote:
Tnx a lot for u tips!!
I just used all (or almost all) u suggestion, but what i really need is some
help in the "logic" of the script.
I mean at the moment I have this 3 arrays:
@G1 = (["alfa" , "10"], ["beta" , "11"]);
@L1 = (["alfa" , "10"], ["gamma" , "12"]);
@G2 =('gamma');
I need to found only the unique couple of results: ["beta" , "11"] , couse
[gamma 12] and [gamma] have to be excluded.
#!/usr/bin/perl
use strict;
use warnings;
my @G1 = (["alfa" , "10"], ["beta" , "11"]);
my @L1 = (["alfa" , "10"], ["gamma" , "12"]);
my @G2 =('gamma');
# populate a hash with the elements of G1
my %unique;
for my $e1 ( @G1 ) {
$unique{$e1->[0]} = $e1->[1];
}
# add elements in L1 not in G1
# delete elements in both
my %overlap;
for my $e2 ( @L1 ) {
my( $key, $val ) = @$e2;
if( exists $unique{$key} ) {
$overlap{$key} = 1;
delete $unique{$key};
}else{
$unique{$key} = $val;
}
}
print "Overlap: ", join(', ',keys %overlap),"\n";
print "Unique: ", join(', ',keys %unique),"\n";
# eliminate unique elements also in G2
for my $e3 ( @G2 ) {
delete $unique{$e3};
}
print "Remains: ", join(", ", keys %unique), "\n";
--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/