• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 211
  • Last Modified:

Searching an array, using another array.

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
cide
Asked:
cide
  • 4
  • 3
1 Solution
 
tpryorCommented:
answer to follow
0
 
tpryorCommented:
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
 
ozoCommented:
@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!

 
ozoCommented:
#actually, tpryor's answer doesn't work on
@food = ( "hamburger", "apple", "pizza", "hamburger", "banna", "icecream", "yogurt","hamburger")
0
 
tpryorCommented:
works fine for me, what seems to be the problem?
0
 
ozoCommented:
Did you actually try it with "hamburger", "pizza" reversed to "pizza", "hamburger"?
0
 
tpryorCommented:
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now