Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
Oops! You are mistaken...
my @test = qw(camel aplaca peguin aplaca monkey camel camel"); my @redundant; for (@test) { next if grep ("$_", @redundant); push (@redundant, $_); } print "@redundant\n";
Prints: camel

Update: zdog asked about this in a private CB message so here goes. tomhukins is not quite correct in his description of the problem. What happens is this:

grep ("$_", @redundant) in a scalar context returns the number of times the expression $_ is true for all the elements of @redundant. But the first time through, there are no elements in @redundant so $_ is evaluated zero times.

So the first time through the loop, the "next if" fails and "camel" is pushed onto @redundant. Each time through the loop after than, @redundant does contain an element and the grep in a scalar context returns 1 meaning "for the one element in @redundant, $_ (the element 'camel') is true."

(Note: I am aware of the discussion about whether to say that grep always returns a list but that a list in scalar context evaluates to the number of elements or whether to say that grep in a scalar context returns a number. perlfunc says: "In scalar context, [grep] returns the number of times the expression was true.")


In reply to Re: (zdog) Re: Finding Redundant values in arrays by dvergin
in thread Finding Redundant values in arrays 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!
  • 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, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others examining the Monastery: (10)
    As of 2014-10-31 09:28 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      For retirement, I am banking on:










      Results (216 votes), past polls