Solved

Searching an array, using another array.

Posted on 1998-06-23
7
203 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 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…

738 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