Hi I have two arrays :
@arcb= (450,625,720,645);
@arca=(625,645);
I need to remove the elements of @arca from elements of @arcb so that the content of @arcb will be (450,720).
Can anyone sugget me how to perform this operation?
The code I have used is this :
my @arcb= (450,625,720,645);
my @arca=(625,645);
foreach my $a (@arca){
for(my $i=0;$i<scalar(@arcb);$i++){
delete $arcb[$i] if($a==$arcb[$i]);
}
}
my $length = @arcb;
print "@arcb";
print "length is $length \n";
The problem is when I used this ,the length of the array is not changing even after the deletion
Deleting an array element effectively returns that position of
the array to its initial, uninitialized state. Subsequently
testing for the same element with exists() will return false.
Also, deleting array elements in the middle of an array will
not shift the index of the elements after them down. Use
splice() for that. See "exists".
Excerpt from perldoc -f delete, q.v. See also perldoc perldoc to see how to solve problems such as this.
As you can read here you'd better no more use delete
Instead, you can use the push instruction
Hereafter please find a solution using push and an extra array to do the trick :
#!/usr/bin/perl
use strict;
my @arcb=(450,625,720,645);
my @arca=(625,645);
my @arcc;
foreach my $b (@arcb) {
my $flg=0;
foreach my $a (@arca) {
if($a==$b) {
$flg=1;
last;
}
}
push(@arcc,$b) if($flg==0);
}
@arcb = @arcc;
my $length = @arcb;
print "@arcb";
print "length is $length \n";