Solved

Remove element from one array that are in another

Posted on 2006-11-02
3
168 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

We all have limited time to study long and complicated information about RAID theories, but you may be interested as to how RAID 5 works. We made it simple for you by providing the shortest and easiest explanation ever.   First we need to remind …
Problem description :  Some external hard disks / USB flash drives do not show actual space as mentioned in the factory settings. This is a common problem when you use an 8 GB USB drive to make it bootable to install a firmware/ driver on a serv…
This tutorial will walk an individual through the process of installing the necessary services and then configuring a Windows Server 2012 system as an iSCSI target. To install the necessary roles, go to Server Manager, and select Add Roles and Featu…
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…

910 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now