Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

(zdog) Re: Finding Redundant values in arrays

by zdog (Priest)
on Jun 08, 2001 at 00:01 UTC ( [id://86708]=note: print w/replies, xml ) Need Help??


in reply to Finding Redundant values in arrays

Updated with info from below, but still an incorrect answer:

for my $test (@test) { push (@redundant, $test) unless grep { $test eq $_ } @redundant; }

Zenon Zabinski | zdog | zdog7@hotmail.com

Replies are listed 'Best First'.
Re: (zdog) Re: Finding Redundant values in arrays
by dvergin (Monsignor) on Jun 08, 2001 at 00:08 UTC
    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.")

      This is broken, notice that it just prints the first element of the @test array? So aplaca doesn't print. In fact if you put peguin as the first element of test it prints peguin. The problem is that you're trying to be to terse. The $_ that you're using in the grep is not the $_ that aliases an element of @test, it's the $_ that aliases an element of @redundant (grep uses $_ too). So you're saying next if there's an element of redundant that matches an element of redundant. Probably not really what your were thinking of.

      Update: Well, it was broken. I started getting down votes for this node, I guess 'cause it's no longer applicable.

      :) Ira

      "So... What do all these little arrows mean?"
      ~unknown

Re: (zdog) Re: Finding Redundant values in arrays
by tomhukins (Curate) on Jun 08, 2001 at 00:06 UTC

    This won't work. The for (@test) line iterates through each element of @test, but then you skip each element with the grep you're using. In English you're saying: for each element in the array, only keep that element if it is not in the array.

    Also, there is no need to place quotes round $_.

    Update: This is wrong. dvergin explains what really happens in Re: (zdog) Re: Finding Redundant values in arrays.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://86708]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (5)
As of 2024-04-25 09:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found