Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: weird array processing question

by Fletch (Bishop)
on May 05, 2011 at 20:53 UTC ( [id://903240]=note: print w/replies, xml ) Need Help??


in reply to weird array processing question

It almost sounds like you want something like this (warning: destructive to the array since it removes items off the beginning because they've already been compared):

my @array = ( 1, 2, 3, 4, 1, 2, 3 ); my $matches = 0; while( @array ) { my $candidate = shift @array; for( @array ) { $matches++ if $candidate == $_ } } print "Got $matches matches\n";

Update: I like toolic's approach below better as it solves the problem "does my list have more than three duplicates of any one element" without as many comparisons; original question was fuzzy so . . .

The cake is a lie.
The cake is a lie.
The cake is a lie.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (4)
As of 2024-04-20 00:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found