http://qs1969.pair.com?node_id=560454

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

hello monks,

I have a array, which values are duplicated once in the same array. I need to remove the second occurence value. I have tried as below, but not getting result. Pls help me to solve this

Thanks

Siva

@content = split(/ /, $content); @Original = @content; for ($i=0; $i<=$#content; $i++) { for ($j=0; $j<$i; $j++) { print "$j\n"; if ($content[$i] == $content[$j]) { $Original[$i] = ""; } } } print "@processing";

Replies are listed 'Best First'.
Re: remove duplicate value in array
by borisz (Canon) on Jul 11, 2006 at 14:03 UTC
Re: remove duplicate value in array
by Nevtlathiel (Friar) on Jul 11, 2006 at 13:46 UTC
    You could use a hash as this will ensure that your keys are unique, or you could searching for "array" and "duplicate". That gives plenty of results.

    ----------
    My cow-orkers were talking in punctuation the other day. What disturbed me most was that I understood it.

Re: remove duplicate value in array
by davorg (Chancellor) on Jul 11, 2006 at 13:47 UTC

    This is a FAQ.

    --
    <http://dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

      This is a test reply
Re: remove duplicate value in array
by Fletch (Bishop) on Jul 11, 2006 at 13:46 UTC

    When you want distinct values think hash.

    my %seen; @content = grep !($seen{$_}++), split( / /, $content );
Re: remove duplicate value in array
by VSarkiss (Monsignor) on Jul 11, 2006 at 13:52 UTC

      And of course, the nice thing about this particular solution is you can still tell if there were any duplicate keys ... in such cases, the value of $seen{$key} will be greater than 1.


      No good deed goes unpunished. -- (attributed to) Oscar Wilde
      It doesn't do quite the same thing. Fletch's solution preserves order. Yours does not.
Re: remove duplicate value in array
by McDarren (Abbot) on Jul 11, 2006 at 13:52 UTC
Re: remove duplicate value in array
by planetscape (Chancellor) on Jul 11, 2006 at 21:50 UTC
      @content = qw(1 2 3 1 2 4 2 5 4 3); print "arr @content\n"; for ($i=0; $i<=$#content; $i++) { my $count = 0; for ($j=$i+1; $j<$#content+1; $j++) { if ($content[$i] == $content[$j]) { $count++; } if ($content[$i] == $content[$j] && $count == 1) { $content[$j] = ""; } } } print "@content";

      Code tags added by GrandFather

Re: remove duplicate value in array
by gasho (Beadle) on Jul 11, 2006 at 19:29 UTC
    ###################################################################### +####### # Remove duplicated array members # For example if you have an array @D=qw(1 2 1 3 1 4 1 5) # It will return an array [1 2 3 4 5] # Usage @Clean=nonDuplicatedArray(@D); sub nonDuplicatedArray #(@DuplicatedArray) { my @Duplicated=@_; my %seen=(); my (@NonDuplicatedArray,@Unique); @Unique = grep {! $seen{$_}++} @Duplicated; @NonDuplicatedArray = sort(@Unique); return @NonDuplicatedArray; } or @ARR2 =('p1','p2','p3','p1','p2','p1'); @unique = grep { ++$count{$_} < 2 } @ARR2;