Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Removing duplicates from an array

by Win (Novice)
on Jan 02, 2008 at 16:29 UTC ( #659995=perlquestion: print w/replies, xml ) Need Help??

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

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: Removing duplicates from an array
by marto (Cardinal) on Jan 02, 2008 at 16:47 UTC
Re: Removing duplicates from an array
by cdarke (Prior) on Jan 02, 2008 at 17:00 UTC
    To try to answer your update. The subroutine builds a hash using the elements of the input array as keys (the values are just set to 1, and not used). Since hash keys are unique, any duplicates will just overwrite the previous value. It then dumps the keys (using keys), and implicitly returns the resulting array. Use it thus:
    my @no_duplicates = r(@original_array);
      my @no_duplicates = r(@original_array);
      Please, note that this method may not preserve the original order of the array values. But uniq function from List::MoreUtils does, as mentioned in perlfaq4 and PBP.
Re: Removing duplicates from an array
by blazar (Canon) on Jan 02, 2008 at 21:41 UTC
    I wish to check that the array @regions_A_array does not contain any duplicates and if it does I wish to remove the duplicate.

    I personally believe I can't fu**ing believe it: Win, I'm sorry to quote myself, but since you have been extensively harassing the Monastery with remove-duplicates-kinda questions, I'm directing you to one of the answers I gave you back then. I'm astonished and disappointed that you're starting this pantomime over again.

    To make this thread minimally interesting (since you can find an actual answer elsewhere) I'll point out that with 5.10 you can easily enough, syntactic-pov-wise, avoid the use of a hash:

    #!/usr/bin/perl use strict; use warnings; use List::Util 'shuffle'; use 5.010; my @new; $_ ~~ \@new or push @new, $_ for shuffle map { ($_) x rand 100 } 'a'..'e'; say "@new"; __END__

    But I wouldn't recommend that!

    --
    If you can't understand the incipit, then please check the IPB Campaign.
Re: Removing duplicates from an array
by cLive ;-) (Prior) on Jan 03, 2008 at 07:45 UTC
    Um. Any reason why you're not filtering duplicates out in the DB query (which you don't provide, so I'm guessing)? For example, by using DISTINCT (if using MySQL).
Re: Removing duplicates from an array
by gasho (Beadle) on Jan 02, 2008 at 18:09 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; }
    (: Life is short enjoy it :)

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (3)
As of 2022-12-09 22:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?