Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > remove redundant elements in an array

Reply
Thread Tools

remove redundant elements in an array

 
 
Yiping Zhan
Guest
Posts: n/a
 
      06-19-2004
Say, @a = qw(1 2 3 1 4 5);

What is a good way to get (1 2 3 4 5)?

Thanks,

-- Newbie
 
Reply With Quote
 
 
 
 
Matt Garrish
Guest
Posts: n/a
 
      06-19-2004

"Yiping Zhan" <(E-Mail Removed)> wrote in message
news(E-Mail Removed)...
> Say, @a = qw(1 2 3 1 4 5);
>
> What is a good way to get (1 2 3 4 5)?
>


Use a hash to remove the duplicates and then just sort the keys if you want
to put them back in a array:

my %unique = map { $_ => 1 } @a;

my @b = sort keys %unique;

Matt


 
Reply With Quote
 
 
 
 
Bob Walton
Guest
Posts: n/a
 
      06-19-2004
Yiping Zhan wrote:

> Say, @a = qw(1 2 3 1 4 5);
>
> What is a good way to get (1 2 3 4 5)?

....
> -- Newbie
>


@b{@a}=1;@b=sort keys %b;

--
Bob Walton
Email: http://bwalton.com/cgi-bin/emailbob.pl

 
Reply With Quote
 
Jürgen Exner
Guest
Posts: n/a
 
      06-19-2004
Yiping Zhan wrote:
> Say, @a = qw(1 2 3 1 4 5);
>
> What is a good way to get (1 2 3 4 5)?


Please see "perldoc -q duplicate" and "perldoc -q sort".

jue


 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      06-19-2004
Bob Walton <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> Yiping Zhan wrote:
>
> > Say, @a = qw(1 2 3 1 4 5);
> >
> > What is a good way to get (1 2 3 4 5)?

> ...
> > -- Newbie
> >

>
> @b{@a}=1;@b=sort keys %b;


Assigning 1 to the hash slice is a little misleading, don't you think?
It assigns 1 to what happens to be first in @a, and undef to the rest.
The reader would have to wonder about the special treatment and find
out that it doesn't matter. "@b{ @a} = ()" is clearer.

Anno

 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      06-19-2004
Yiping Zhan <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> Say, @a = qw(1 2 3 1 4 5);
>
> What is a good way to get (1 2 3 4 5)?


my %h;
my @a = grep ! $h{ $_} ++, qw( 1 2 3 1 4 5);

Anno
 
Reply With Quote
 
Jeff 'japhy' Pinyan
Guest
Posts: n/a
 
      06-21-2004
[posted & mailed]

On Fri, 18 Jun 2004, Yiping Zhan wrote:

>Say, @a = qw(1 2 3 1 4 5);
>
>What is a good way to get (1 2 3 4 5)?


Tie::Array::Unique is a rather effortless way.

--
Jeff Pinyan RPI Acacia Brother #734 RPI Acacia Corp Secretary
"And I vos head of Gestapo for ten | Michael Palin (as Heinrich Bimmler)
years. Ah! Five years! Nein! No! | in: The North Minehead Bye-Election
Oh. Was NOT head of Gestapo AT ALL!" | (Monty Python's Flying Circus)

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to remove duplicate elements in a 2D array Li Chen Ruby 19 09-26-2009 05:10 PM
Macro -> How to remove redundant code MaciekL C Programming 1 09-16-2009 04:37 PM
Array#uniq with Hash elements... can't remove duplicates andrea Ruby 2 05-12-2008 08:30 AM
XML elements to JavaScript Array elements Conversion P XML 1 07-07-2006 09:08 PM
redundant switches / redundant server NICs Stuart Kendrick Cisco 4 08-10-2004 08:54 PM



Advertisments