Saturday, January 29, 2011

perl: get mismatch one array element into another array element

#!/usr/bin/perl -w

@content = (1,2,3,4,5,6,7,8,9,'a','b','c','d','e');
@alphabets = ('a','b','c','d','e','f','g','h');

# create a hash with elements of @alphabets as keys and assign them a value 1
%alpha_hash =();
foreach my $alpha (@alphabets)
{
   $alpha_hash{$alpha} = 1;
}

foreach $elem (@content)
{
    if(! $alpha_hash{$elem}) # it's not in %alpha_hash, so this is what we need
    {    
        print $elem."\n";          #it is the one not present in $alphabets
    }
}

No comments: