Solved

Remove element from one array that are in another

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Each year, investment in cloud platforms grows more than 20% (https://www.immun.io/hubfs/Immunio_2016/Content/Marketing/Cloud-Security-Report-2016.pdf?submissionGuid=a8d80a00-6fee-4b85-81db-a4e28f681762) as an increasing number of companies begin to…
The question appears often enough, how do I transfer my data from my old server to the new server while preserving file shares, share permissions, and NTFS permisions.  Here are my tips for handling such a transfer.
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…

820 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