Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

remove duplicate value in array

by sivaramanm (Acolyte)
on Jul 11, 2006 at 13:42 UTC ( #560454=perlquestion: print w/replies, xml ) Need Help??

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;

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (1)
As of 2023-06-02 01:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?