Solved

Searching an array, using another array.

Posted on 1998-06-23
7
171 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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
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…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

708 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

21 Experts available now in Live!

Get 1:1 Help Now