Remove repetitions in Array

Discussion in 'Perl' started by oleber, Apr 28, 2007.

  1. oleber

    oleber New Member

    Joined:
    Apr 23, 2007
    Messages:
    37
    Likes Received:
    2
    Trophy Points:
    0
    Occupation:
    Software Developer (Perl, C/C++ and Java)
    Location:
    Hamburg, Germany
    Home Page:
    http://oleber.freehostia.com/
    The normal idea is to have an hash table, iterate over the array and using the elements as keys.

    Something like ::(
    Code:
    my @array = (1,5,3,7,9,1,4,5,7,6,7);
    
    my %hash;
    
    foreach my $element (@array) {
    	$hash{$element}++
    }
    
    my @arrayNoRep = keys %hash;
    print join(", ", @arrayNoRep);
    Lets simplify the loop :cool:
    Code:
    my @array = (1,5,3,7,9,1,4,5,7,6,7);
    
    my %hash;
    $hash{$_}++ foreach @array;
    
    my @arrayNoRep = keys %hash;
    print join(", ", @arrayNoRep);
    But do we need the loop? :confused:
    Code:
    my @array = (1,5,3,7,9,1,4,5,7,6,7);
    
    my %hash;
    @hash{@array} = ();
    
    my @arrayNoRep = keys %hash;
    print join(", ", @arrayNoRep);
    And throwing away the hash defeniton. :eek:
    Code:
    my @array = (1,5,3,7,9,1,4,5,7,6,7);
    
    my @arrayNoRep = keys %{{map {$_=>0} @array}};
    
    print join(", ", @arrayNoRep);
    I love this one line in Perl.:D
     
  2. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in
    That's really cool! :D
     
  3. oleber

    oleber New Member

    Joined:
    Apr 23, 2007
    Messages:
    37
    Likes Received:
    2
    Trophy Points:
    0
    Occupation:
    Software Developer (Perl, C/C++ and Java)
    Location:
    Hamburg, Germany
    Home Page:
    http://oleber.freehostia.com/
  4. msmani_1980

    msmani_1980 New Member

    Joined:
    Nov 30, 2009
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Remove repetitions in Array without loop

    Code:
    my @array = (1,5,3,7,9,1,4,5,7,6,7);
    @t{@array}=();
    print join sort keys %t,",";
    Result : 1, 3, 4, 5, 6, 7, 9
     
  5. oleber

    oleber New Member

    Joined:
    Apr 23, 2007
    Messages:
    37
    Likes Received:
    2
    Trophy Points:
    0
    Occupation:
    Software Developer (Perl, C/C++ and Java)
    Location:
    Hamburg, Germany
    Home Page:
    http://oleber.freehostia.com/
    So, What did you add to the subject msmani_1980?
     
  6. PradeepKr

    PradeepKr New Member

    Joined:
    Aug 24, 2010
    Messages:
    24
    Likes Received:
    0
    Trophy Points:
    0
    Home Page:
    http://www.expertsguide.info
    I couldn't get this line in the 3rd code block
    @hash{@array} = ();

    hash is defined with % in the previous line (%hash) and then it is seen with @ which is meant for array. Can somebody explain me, what exactly "@", prepended to hashname, does?
     
  7. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in
    "@", prepended to hashname means we are accessing a hash slice i.e. a group of elements instead of one element a time.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice