@arr = (1,2,3,1); $hash{$_} = $_ foreach(@arr); @brr = keys %hash; print "@brr";
  • Comment on To remove duplicate value from an array

Replies are listed 'Best First'.
Re: To remove duplicate value from an array
by Ratazong (Monsignor) on Feb 01, 2010 at 10:51 UTC
Re: To remove duplicate value from an array
by jdrago999 (Pilgrim) on Feb 12, 2010 at 16:59 UTC

    As someone once said, "You can program C in any language."

    Here is a more Perl-ish solution:

    #!/usr/bin/perl -w use strict; use warnings 'all'; my @arr = ( 1, 2, 3, 1 ); my %saw = ( ); my @unique = grep { ! $saw{$_}++ } @arr; use Data::Dumper; warn Dumper( \@unique );

    That one line...
    my @unique = grep { ! $saw{$_}++ } @arr;
    ...is where all the action is.

    We create a hash (%saw) and the only items that make it into @unique are elements that have not been filtered through grep { $saw{$_}++ }.

    Kudos to you though for really thinking through this problem. Just goes to show - There's More Than One Way To Do It!

Re: To remove duplicate value from an array
by tirwhan (Abbot) on Feb 01, 2010 at 10:46 UTC

    Your problem is on line 42.

    Oh, I'm sorry, was there a question somewhere in there? One which can't be answered by checking the Q&A section? If so, maybe you should make it slightly more explicit.

    Update: right, posted in CUFP (thanks for the nudge BrowserUk), so no real need for a question. It's still a bit of waste of bits if you ask me, given the existence of far better solutions (which the OP must be aware of, since he posted a more buggy version of this there only minutes late).


    All dogma is stupid.