Solved

Remove element from one array that are in another

Posted on 2006-11-02
3
170 Views
Last Modified: 2010-04-03
I have 2 arrays
my @array1 = qw/test1.txt test5.txt test8.txt/;
my @array2 = qw/test1.txt test2.txt test3.txt test4.txt test5.txt test6.txt test7.txt test8.txt test9.txt/;

I'm trying to remove any in @array1 out of @array2 and they make a list of @array2.
So array2 = qw/test2.txt test3.txt test4.txt test6.txt test7.txt test9.txt/;
and then the content of array2 would go into a list called list_test.

Sample code doesn't work.
Note: One question about my code. Once it hits the if I start pushing the number 1 in the array, why?  
my @field1;

foreach my$test1 (@array1) {
 chomp my $test1;
   foreach $test2 (@array2) {
   chomp $test2;
     if ($test2 =! /$test1/) {
       push (@field1, $test2);
     }
   }
 }

my $file1 = "home/list_test";
open(OUTPUT, ">$file1") || die "can't open $file1 $!\n";
foreach (@field1) {
  print OUTPUT "$_\n";  
}
0
Comment
Question by:ckaspar
3 Comments
 
LVL 77

Accepted Solution

by:
arnold earned 500 total points
ID: 17864851
First, this is not the correct forum for the question since this is not a storage related question.  Second, your logic is incorrect as is the syntax. presumably this is perl/php.

The following should do the trick.

foreach my $test1 (@array2) {

 my $found=0;
   foreach $test2 (@array1) {
   chomp $test2;
     if ($test2 == $test1) {
       $found=1;
        last;
     }
   }
   push (@field1, $test1) if ( $found ==0) ;
 }

my $file1 = "home/list_test";
open(OUTPUT, ">$file1") || die "can't open $file1 $!\n";
foreach (@field1) {
  print OUTPUT "$_\n";  
}

0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Hi, I've made you some graphics for a better understanding how RAID works. First of all, there are two ways a raid can be generated: - By hardware - By software What does that mean? This means: If you have a hardware RAID controller, there…
The article will include the best Data Recovery Tools along with their Features, Capabilities, and their Download Links. Hope you’ll enjoy it and will choose the one as required by you.
This video teaches viewers how to encrypt an external drive that requires a password to read and edit the drive. All tasks are done in Disk Utility. Plug in the external drive you wish to encrypt: Make sure all previous data on the drive has been …
This Micro Tutorial will teach you how to reformat your flash drive. Sometimes your flash drive may have issues carrying files so this will completely restore it to manufacturing settings. Make sure to backup all files before reformatting. This w…

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question