remove redundant elements in an array

Discussion in 'Perl Misc' started by Yiping Zhan, Jun 19, 2004.

  1. Yiping Zhan

    Yiping Zhan Guest

    Say, @a = qw(1 2 3 1 4 5);

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

    Thanks,

    -- Newbie
    Yiping Zhan, Jun 19, 2004
    #1
    1. Advertising

  2. Yiping Zhan

    Matt Garrish Guest

    "Yiping Zhan" <> wrote in message
    news:p...
    > 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
    Matt Garrish, Jun 19, 2004
    #2
    1. Advertising

  3. Yiping Zhan

    Bob Walton Guest

    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
    Bob Walton, Jun 19, 2004
    #3
  4. 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
    Jürgen Exner, Jun 19, 2004
    #4
  5. Yiping Zhan

    Anno Siegel Guest

    Bob Walton <> 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
    Anno Siegel, Jun 19, 2004
    #5
  6. Yiping Zhan

    Anno Siegel Guest

    Yiping Zhan <> 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
    Anno Siegel, Jun 19, 2004
    #6
  7. [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)
    Jeff 'japhy' Pinyan, Jun 21, 2004
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. MaciekL

    Macro -> How to remove redundant code

    MaciekL, Sep 16, 2009, in forum: C Programming
    Replies:
    1
    Views:
    259
    Keith Thompson
    Sep 16, 2009
  2. andrea
    Replies:
    2
    Views:
    122
    andrea
    May 12, 2008
  3. Li Chen
    Replies:
    19
    Views:
    238
    Li Chen
    Sep 26, 2009
  4. Bryan
    Replies:
    8
    Views:
    134
    Brian McCauley
    May 7, 2004
  5. PerlFAQ Server
    Replies:
    0
    Views:
    171
    PerlFAQ Server
    Mar 1, 2011
Loading...

Share This Page