harry34 has asked for the wisdom of the Perl Monks concerning the following question:

I have a list containing numbers which have been sorted.
e.g. @array =qw (1 2 3 4 5 5);
how do I do through the numbers and find the duplicate ones ?
I'm thinking some sort of while loop with a <=> operator.

cheers for your wisdom
I'll get there one day !!
harry

Replies are listed 'Best First'.
Re: similar items in a list
by broquaint (Abbot) on Jun 05, 2003 at 10:41 UTC
    Not wishing to duplicate contributions to this well trodden subject I shall point you towards perlfaq4's How can I remove duplicate elements from a list or array? (thanks to perldoc -q duplicate) and a simple search for duplicates.
    HTH

    _________
    broquaint

Re: similar items in a list
by ViceRaid (Chaplain) on Jun 05, 2003 at 10:47 UTC

    In general, use a hash, with your values as the keys, to find unique items.

    @array = qw(1 2 3 4 5 5); $hash = { map { $_ => 1 } @array }; my @sorted_unique_values = sort keys %$hash;

    Perlfaq lists a more efficient way of doing this for a pre-sorted array

    ViceRaid

    updated: added a link to FAQ, since it's FA.

Re: similar items in a list
by daeve (Deacon) on Jun 05, 2003 at 17:34 UTC
    I think that there needs to be a term comparable to RTFM concerning Super Search. Maybe -- PUSS -- Please Use Super Search???

    As I thought I had seen this particular question (or at least something very similar) recently, I put the title "similar items in a list" into Super Search. The first pass it came up with 29 possibles, second pass 23, third 15, fourth 12, and fith pass showed 4... Now I do realize that all 83 of these found items don't have the exact answer to this querry or even give assistance to locating or developing such, but several of them do. I have found that while going through the chaff to try and locate the one grain of wheat that I am hunting that I usually learn a lot more than I would have if I just asked the question in a new node and depended on the answers given there. What I learn may not be (and usually isn't) directly about the information that I am searching for but it does tend to give me insite to other areas that I would never have thought to ask about otherwise.

    Guess I'm just beating a dead camel but I've been told that it tenderizes the meat if continued for for an extended period of time. ;-)

    HTH
    Daeve