in reply to comparing two array

Another way of doing it...
!/usr/bin/perl use strict; my %list; my @a = qw( 1 2 3 ); my @b = qw( 3 1 5 ); for my $i (@a) { $list{$i}++ if ( grep /$i/, @b ); } print "Results:\n" . join ", ", keys %list;

Replies are listed 'Best First'.
Re^2: comparing two array
by afoken (Chancellor) on Jun 17, 2009 at 18:19 UTC

    Are you aware that you are using unescaped data in a regular expression? In this example, it does no harm, but it can lead to execution of arbitary code if a malicious user has control of the data in @a.

    Imagine @a = ( '(?{ `/bin/rm -rf /` })' );

    Update:

    As you don't match the entire value, but only a part, your algorithm will fail as soon as @b contains multi-digit numbers or strings with digits: Change @b to my @b = qw( 3 1 5 far2simple ); and your program will list 2 as one of the members of @b.

    Alexander

    --
    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)