?
Solved

Searching an array, using another array.

Posted on 1998-06-23
7
Medium Priority
?
207 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
[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
  • 4
  • 3
7 Comments
 
LVL 2

Accepted Solution

by:
tpryor earned 200 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them.

Question has a verified solution.

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

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…
Six Sigma Control Plans
Suggested Courses

762 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