Solved

Remove element from one array that are in another

Posted on 2006-11-02
3
175 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 78

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Ever notice how you can't use a new drive in Windows without having Windows assigning a Disk Signature?  Ever have a signature collision problem (especially with Virtual Machines?)  This article is intended to help you understand what's going on and…
In this article we will learn how to backup a VMware farm using Nakivo Backup & Replication. In this tutorial we will install the software on a Windows 2012 R2 Server.
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…

739 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