2005/10/8, Jabir Ahmed <[EMAIL PROTECTED]>:
> Hello everyone,
>
> I have a pretty simple problem
>
> example :
>
> @a=qw( 1 2 3 4 5 6 7 );
> $b=5;
>
> now i just want to check if the value of $b i.e 5
> exists in the array @a or not
>
> Q 1) is there any way other than looping through the
> array?
>
> Q2 ) is there any predefined function to check the
> existance of the element in the array..
>
> thanks in advance
>
> jabir ahmed

I think this should be the anwser to both of your questions.
You can use grep:
#!/usr/bin/perl
use warnings;
use strict;
my $b = 5;
my $x; # how many times
my @a = qw(1 2 3 4 5 6 7);
if($x=grep { /$b/ } @a) { print "$b exists in [EMAIL PROTECTED] $x times\n"; }
else { print "$b does not exist\n"; }

grep goes through each element of an array and returnes the one that
match the regular expression. I think if you need some complicated
manouvers you can also use map { } @array check it in:
perldoc -f map

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to