array - removing duplicates

Discussion in 'Perl Misc' started by Jerry Preston, Nov 15, 2004.

  1. @dat = qw( 9 5 12 14 17 20 11 2 10 15 6 5 14 17 16 25 12 24 );

    I want to removes all the duplicates. I am currently using:

    @dat = grep { !$dat{ $_ }++ } @dat;

    99% of the time I get an empty array. Why? Is their a better/100% way?

    Thanks,

    Jerry
     
    Jerry Preston, Nov 15, 2004
    #1
    1. Advertising

  2. Jerry Preston

    Tony Curtis Guest

    >> On Sun, 14 Nov 2004 21:43:34 -0600,
    >> "Jerry Preston" <> said:


    > @dat = qw( 9 5 12 14 17 20 11 2 10 15 6 5 14 17 16 25 12 24
    > ); I want to removes all the duplicates. I am currently
    > using:


    > @dat = grep { !$dat{ $_ }++ } @dat;


    > 99% of the time I get an empty array. Why?


    I guess something is broken on your system. Why "99%"? What
    happens the other 1% of the time?

    Or there's something else going on you're not telling us about
    (maybe because you thought it wasn't important and
    accidentally omitted salient details of a larger problem).

    hth
    t
     
    Tony Curtis, Nov 15, 2004
    #2
    1. Advertising

  3. Bob,

    Syntax!

    Thanks,

    Jerry


    "Bob Walton" <> wrote in message news:41982ead_5@127.0.0.1...
    > Jerry Preston wrote:
    >
    > > @dat = qw( 9 5 12 14 17 20 11 2 10 15 6 5 14 17 16 25 12 24 );
    > >
    > > I want to removes all the duplicates. I am currently using:
    > >
    > > @dat = grep { !$dat{ $_ }++ } @dat;
    > >
    > > 99% of the time I get an empty array. Why? Is their a better/100% way?

    > ...
    > > Jerry
    > >
    > >

    > Your test program, verbatim as above, generates the proper list of
    > numbers without duplicates 100% of the times I have run it (at least
    > half a dozen times). The problem you are having probably lies elsewhere.
    > --
    > Bob Walton
    > Email: http://bwalton.com/cgi-bin/emailbob.pl
     
    Jerry Preston, Nov 15, 2004
    #3
  4. Jerry Preston wrote:
    > @dat = qw( 9 5 12 14 17 20 11 2 10 15 6 5 14 17 16 25 12 24 );
    >
    > I want to removes all the duplicates. I am currently using:


    Do you have any particular problem with "perldoc -q duplicate"?
    " How can I remove duplicate elements from a list or array?"

    jue
     
    Jürgen Exner, Nov 15, 2004
    #4
  5. Jerry Preston

    Anno Siegel Guest

    Bob Walton <> wrote in comp.lang.perl.misc:
    > Jerry Preston wrote:
    >
    > > @dat = qw( 9 5 12 14 17 20 11 2 10 15 6 5 14 17 16 25 12 24 );
    > >
    > > I want to removes all the duplicates. I am currently using:
    > >
    > > @dat = grep { !$dat{ $_ }++ } @dat;
    > >
    > > 99% of the time I get an empty array. Why? Is their a better/100% way?

    > ...
    > > Jerry
    > >
    > >

    > Your test program, verbatim as above, generates the proper list of
    > numbers without duplicates 100% of the times I have run it (at least
    > half a dozen times). The problem you are having probably lies elsewhere.


    My guess is, the (undeclared) hash %dat still contains data from an
    earlier run in 99 % of the cases. Solution: use strict.

    Anno
     
    Anno Siegel, Nov 15, 2004
    #5
  6. Jerry Preston

    Uri Guttman Guest

    >>>>> "AS" == Anno Siegel <-berlin.de> writes:

    AS> My guess is, the (undeclared) hash %dat still contains data from an
    AS> earlier run in 99 % of the cases. Solution: use strict.

    i guessed that too. strict won't help unless the declaration is in the
    tightest scope and it will clear the hash before the uniquing line. if
    it is declared outside a loop doing the unique it will have the same
    bug.

    and the OP never stated what was wrong with the results. typical poor
    description of the problem. if we were given some data from a bad result
    then this guess could be made more assuredly or disregarded.

    uri

    --
    Uri Guttman ------ -------- http://www.stemsystems.com
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
     
    Uri Guttman, Nov 15, 2004
    #6
    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. William F. Robertson, Jr.

    Re: Removing duplicates from a DropdownList

    William F. Robertson, Jr., Aug 4, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    589
    brian richards
    Aug 4, 2003
  2. Fred
    Replies:
    15
    Views:
    71,078
    Archer
    Mar 12, 2005
  3. Sam Larbi
    Replies:
    10
    Views:
    292
    Sam Larbi
    Nov 28, 2007
  4. Chuck Remes
    Replies:
    23
    Views:
    361
    Joel VanderWerf
    Jul 20, 2009
  5. Jack
    Replies:
    1
    Views:
    142
    Tad McClellan
    Jun 10, 2006
Loading...

Share This Page