Probably one of the fastest and most reliable ways would be to do the following:

# create a hash representing @array2 my %hash; $hash{$_} = undef foreach (@array2); # grep only leaves what evaluates to true. # if an element of array1 is not in array2, it is # left in place @array1 = grep { not exists $hash{$_} } @array1;

To perform your original approach, you would probably want to use the command splice, like so:

if($array1[$search] eq $array[$search2]){ splice(@array1,$search,1); # remove 1 element from index $search i +n @array1 $search--; # the next element is already at $search. decrement so +when it's incremented we'll have the same value. }


But this implementation is very inefficient.

-nuffin
zz zZ Z Z #!perl

In reply to Re: subtract one array from another by nothingmuch
in thread subtract one array from another by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.