s_gaurav1091 has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks, I have to ensure that my array conatains for eg bin client and server.I want to ensure that if my array contains all the three elements and if all the three elements are present then I want to set a flag. How should I go about it?plz help
  • Comment on comparing some data form the elements of an array

Replies are listed 'Best First'.
Re: comparing some data form the elements of an array
by prasadbabu (Prior) on Feb 09, 2006 at 10:24 UTC

    s_gourav1091, Here is one way to do it.

    use strict; my @all = qw(bin client server all then); my @some = qw(bin client server); #user defined elements my %h; my @c; my $flag; @h{@some} = ( ); # Elements that exist in both arrays exists($h{$_}) and push @c, $_ for @all; if (scalar(@c) == 3) { print "set flag"; $flag = 1; }

    Prasad

Re: comparing some data form the elements of an array
by svenXY (Deacon) on Feb 09, 2006 at 10:26 UTC
    Hi,
    #!/usr/bin/perl use strict; use warnings; my @must_have=qw(abc def ghi); my @to_check = @must_have[0,1]; # only the first two elements my %lookup = map {$_, 1} @to_check; # lookup hash my $flag = 1; for (@must_have) { next if $lookup{$_}; $flag = 0 } print "flag not set" if not $flag

    Regards,
    svenXY