|
[Original]
[Print]
[Top]
|
比如两个数组@a和@b,要剔除@a没有而@b有(@b有而@a没有)的元素,并放到@c里,怎么搞?
例:
@a = (1,2,3,4,5,6);
@b = (2,3,6,7,8,9);
要留下1,2,3,4,5,6这几个元素赋值给@c
应该怎么写?
|
|
|
[Original]
[Print]
[Top]
|
|
[Original]
[Print]
[Top]
|
不是很理解:
这样行吗?
@a = (1,2,3,4,5,6,7);
@b = (2,3,6,7,8,9);
my (%t1,%t2,@c);
undef @t1{ @a };
undef @t2{ @b };
delete @t2{ @a };
@c = keys %t1,keys %t2;
print "@c
";
|
|
|
----
无知便无畏
|
|
[Original]
[Print]
[Top]
|
|
[Original]
[Print]
[Top]
|
可以参考这个
perldoc -q "How do I compute the difference of two arrays?"
|
|
|
----
It's better to burn out than to fade away...
|
|
[Original]
[Print]
[Top]
|
|
[Original]
[Print]
[Top]
|
{
my %hash=();
# Input array_a data into hash table
foreach my $elem ( @a )
{
$hash{ $elem } = 1;
}
# Find and store the different data, compared with array_b
my @diff = grep( !$hash{$_}, @b);
# Append the different data into array_a
@a = (@a, @diff);
# Release memory
#%hash = ();
#@diff =();
} // end block
|
|
|
[Original]
[Print]
[Top]
|
|