You want grep. Here is an example:

@ary = qw(just another perl foo hacker); @no_foo = grep{ $_ ne 'foo' }@ary; print "@no_foo"

The grep function examines the whole of the @ary array, aliasing each value in turn to $_ within the { }. If the value within the { } is true then that element is added to the list that grep returns. In this case grep returns a list of all the values that are not equal to 'foo'. Thus we have used it to remove one value from the array. Although I have assigned the result to the @no_foo array I could have just as easily assigned it back to @ary. This is just a simple example of what grep can do - it is a really handy function and well worth getting familiar with. Because we alias to $_ you can include a match type regex like /foo|bar/ which will return all the elemnts of the array that match either foo or bar.

cheers

tachyon


In reply to Re: finding and deleting item from an array by tachyon
in thread finding and deleting item from an array 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.