removing duplics in an array

Discussion in 'Perl Misc' started by Jerry Preston, Oct 17, 2004.

  1. Hi!

    I am able to remove duplets in a simple array:

    @name = grep { ! $name_{ $_ }++ } @name_;

    But can I do it for each array in a has of arrays?

    @{ $ID{ $id }{ data }} = grep { ! ${ $ID{ $id }{ data }}{ $_ }++ } @{
    $ID{ $id }{ data }};

    Thanks,

    Jerry
     
    Jerry Preston, Oct 17, 2004
    #1
    1. Advertising

  2. Jerry Preston wrote:
    > I am able to remove duplets in a simple array:
    >
    > @name = grep { ! $name_{ $_ }++ } @name_;
    >
    > But can I do it for each array in a has of arrays?


    Yes, of course.

    > @{ $ID{ $id }{ data }} = grep { ! ${ $ID{ $id }{ data }}{ $_ }++ } @{

    ------------------------------------^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    What do you think you access with that? Why would you like the counter
    to be part of the data structure?

    Just do like this:

    for my $id ('id1', 'id2') {
    my %count;
    @{ $ID{$id}{data} } = grep { ! $count{$_}++ } @{ $ID{$id}{data} };
    }

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Oct 17, 2004
    #2
    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. Ben Miller
    Replies:
    2
    Views:
    421
    Jim Gibson
    May 14, 2004
  2. Fred
    Replies:
    15
    Views:
    71,076
    Archer
    Mar 12, 2005
  3. Chris  Chiasson
    Replies:
    6
    Views:
    635
    Richard Tobin
    Nov 14, 2006
  4. laredotornado

    removing an element from an array

    laredotornado, Oct 20, 2008, in forum: Java
    Replies:
    5
    Views:
    357
    Arne Vajhøj
    Oct 26, 2008
  5. Leif Wessman
    Replies:
    3
    Views:
    105
    Gunnar Hjalmarsson
    Aug 29, 2004
Loading...

Share This Page