Solved

Searching an array, using another array.

Posted on 1998-06-23
7
179 Views
Last Modified: 2010-03-05
Hi,
    I'd like to know how I would go about searching an array for elements which are also contained in a second array and then remove the elements found.
0
Comment
Question by:cide
  • 4
  • 3
7 Comments
 
LVL 2

Accepted Solution

by:
tpryor earned 100 total points
ID: 1208140
answer to follow
0
 
LVL 2

Expert Comment

by:tpryor
ID: 1208141
This will work...

############################

@food = ( "hamburger", "apple", "hamburger", "pizza", "banna", "icecream", "yogurt", "hamburger");
@junk_food = ( "hamburger", "pizza", "icecream");

print @food;

for($i = 0; $i <= $#food;  $i++){
      foreach $junk (@junk_food){
            if($junk eq $food[$i]){
                  splice(@food, $i, 1);
            }
      }
}

print "\n";
print @food;

”””””””””””””””””””””

remember to change the if expression depending on what you are evaluating letters, numbers, regex etc...

GL
t
0
 
LVL 84

Expert Comment

by:ozo
ID: 1208142
@junk{@junk_food} = (1)x@junk_food;
@food = grep {!$junk{$_}} @food;
#see also "How  do  I compute the difference of two arrays?  How do I compute the intersection of two arrays?"
#in perldoc perlfaq4
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 84

Expert Comment

by:ozo
ID: 1208143
#actually, tpryor's answer doesn't work on
@food = ( "hamburger", "apple", "pizza", "hamburger", "banna", "icecream", "yogurt","hamburger")
0
 
LVL 2

Expert Comment

by:tpryor
ID: 1208144
works fine for me, what seems to be the problem?
0
 
LVL 84

Expert Comment

by:ozo
ID: 1208145
Did you actually try it with "hamburger", "pizza" reversed to "pizza", "hamburger"?
0
 
LVL 2

Expert Comment

by:tpryor
ID: 1208146
oops, forgot the decrement...

for($i = 0; $i <= $#food;@$i++){
      foreach $junk (@junk_food){
            if($junk eq $food[$i]){
                  splice(@food, $i, 1);
                  $i--;@# Decrement
            }
      }
}

fixed
t
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

895 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

17 Experts available now in Live!

Get 1:1 Help Now