Solved

Remove element from one array that are in another

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

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

Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

Join & Write a Comment

Don't buy such-and-such disk drives, they are not reliable!    Boy, how many times have we read a post from a well-meaning person who had some bad experiences with a drive and now they exclaim the entire company's products can't be trusted.  First, …
Create your own, high-performance VM backup appliance by installing NAKIVO Backup & Replication directly onto a Synology NAS!
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…

705 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

18 Experts available now in Live!

Get 1:1 Help Now