CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Advanced:
How eliminate repeated elements of array

 



mackiew
Deleted

Jan 19, 2001, 6:28 AM

Post #1 of 3 (1147 views)
How eliminate repeated elements of array Can't Post

Exist some function to eliminate repeated elements of an Array ???

Example @array = ('a','b','a','x','x','b','c')

after function(\@array)

@array = ('a','b','x','c')

Thanks






perlplexer
Deleted

Jan 19, 2001, 7:01 AM

Post #2 of 3 (1145 views)
Re: How eliminate repeated elements of array [In reply to] Can't Post

my @array = ('a','b','a','x','x','b','c');
my %uniq = ();

@uniq{@array} = ();
@array = keys %uniq;

print "@array"; # prints: a b x c (not necessarily in this order though)

--perlplexer



sleuth
Enthusiast

Jan 19, 2001, 1:42 PM

Post #3 of 3 (1142 views)
Re: How eliminate repeated elements of array [In reply to] Can't Post

 I just wanted to add one thing even though perlplexer's code works great, only because I thought this way is easier to remember.

@uniques = grep(!$seen{$_}++, @array);
print "@uniques";

That code keeps the order the same also.

No offense or anything perlplexer, I think your awesome.

Sleuth


 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives