in reply to Removal of duplicated element in array.

Hi,
For me the same code is working

my @match_to_array = (a,b,c,a,d,c,b,r); my %seen = (); my @r = (); foreach my $a (@match_to_array) { unless ($seen{$a}) { push @r, $a; $seen{$a}++; } } print "@r"; print "\n@match_to_array";


Can you explain how it is not working , with actual and expected output.

Replies are listed 'Best First'.
Re^2: Removal of duplicated element in array.
by 2teez (Vicar) on May 06, 2013 at 14:36 UTC

    Hi Rahul6990 ,
    this line :my @match_to_array = (a,b,c,a,d,c,b,r); #Oops
    should either be
    my @match_to_array = ('a','b','c','a','d','c','b','r');
    OR
    my @match_to_array = qw(a b c a d c b r);

    If you tell me, I'll forget.
    If you show me, I'll remember.
    if you involve me, I'll understand.
    --- Author unknown to me
Re^2: Removal of duplicated element in array.
by tty1x (Novice) on May 06, 2013 at 14:16 UTC
    dddd@fgg.com
    cccc@fgg.com
    eeee@fgg.com
    dddd@fgg.com
    
    The above is both the input and output. The output is still the same after running the code.