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

Hi Monks, I basically want to extract one of every individual item in a list. e.g.
my @array = ('11','54','23','78','54','54','78'); # i want to extract 11, 54, 23 and 78.
I tried this with  @numbers = grep {! $seen{$_} ++} @data; but this seems to only extract the numbers if there is only one copy of them. How can I alter this to do what I want?? CHEERS

Replies are listed 'Best First'.
Re: extracting redundant and unique items
by gmax (Abbot) on May 14, 2003 at 16:22 UTC

    Or, alternatively, use an anonymous hash to get the unique values, in a classical way.

    my @unique = keys %{{map {$_,undef} @array}}; print join ",", sort {$a <=> $b} @unique; __END__ 11,23,54,78
     _  _ _  _  
    (_|| | |(_|><
     _|   
    
Re: extracting redundant and unique items
by broquaint (Abbot) on May 14, 2003 at 16:13 UTC
    my @array = ('11','54','23','78','54','54','78'); print join ', ', grep ++$seen{$_} == 1, @array; __output__ 11, 54, 23, 78

    HTH

    _________
    broquaint

      Your code is just an obfuscation of what the original post said didn't work. Of course, your code works ... but so does the original code. The question's premise is just wrong. sigh

          -- Chip Salzenberg, Free-Floating Agent of Chaos

        Your code is just an obfuscation of what the original post said didn't work
        I'd say my code is clearer as it illustrates that grep is looking for elements that it has only seen once, IMHO at least. As for the question's premise being incorrect, I should've duly noted that their code was working but their question was 'wrong' (as I've done so earlier with the same (or seemingly so) AnonyMonk) but I guess tiredness got the better of me. You post, you learn ;)
        HTH

        _________
        broquaint

Re: extracting redundant and unique items
by bigj (Monk) on May 14, 2003 at 19:01 UTC
    As TMTWTDI, here's a solution with the CPAN module Array::Unique.
    use Array::Unique; my @orig_array = ('11','54','23','78','54','54','78'); tie my @uniq, 'Array::Unique'; @uniq = @orig_array; print "@uniq";

    Greetings,
    Janek

Re: extracting redundant and unique items
by AssFace (Pilgrim) on May 14, 2003 at 16:18 UTC
    I'm sure there has to be a better way to do it than the way I have here, but one way would be to iterate over that array and putting each spot into a hash as the key. Then once that is done, you can get the array of the keys of that hash and you have your unique values.

    Like I said, that is ugly and probably wasteful - but it is the first thing that popped into my head.

    Here is some totally untested code:
    my @array = ('11','54','23','78','54','54','78'); my %hash; foreach $thing (@array){ #increment it in case for some reason you later want a # count of how many each are in the orig array $hash{$thing}++; } my @unique = keys %hash;


    -------------------------------------------------------------------
    There are some odd things afoot now, in the Villa Straylight.
      AssFace,
      I have been told that if you are going to throw away the values it is better not to even bother assigning the hash keys:
      $hash{$thing}++; versus $hash{$thing} = undef;
      I am not one for cargo cult programming. I honestly want to know why something is a better idiom for something than just doing it. With this said, I haven't thoroughly investigated this. I did do some benchmarks and the undef came out faster. Additionally, it makes sense that it isn't allocating any memory or doing any computation of the value.

      Cheers - L~R

        That does make sense - since it doesn't do anything in terms of allocating memory for it and the like.

        I was mainly thinking in case they later wanted to know that there were 15 instances of X, 12 instances of Y, and 2 instances of Z, then the code would have that...

        But since they didn't say anything about wanting that, I suppose it would make more sense to do the undef instead and then later easily add in addition functionality like counting.

        -------------------------------------------------------------------
        There are some odd things afoot now, in the Villa Straylight.