Irfan J Sayed (isayed) wrote:
> Hi All,
>
> I want to search whether specific value/data is there are not in Array.
> For example lets say i have array as follows.
>
> @test = (1,2,3,4,5);
> $rel1=4;
>
> I need to check whether 4 is there or not. I have one method as follows.
>
> if (grep /^$rel1$/, @test)
> {
> print "Result = $rel1\n";
> }
>
> but i think this is not the proper way because if i use grep then it
> would be a platform dependant.
>
> Any proper/recommended way.
grep is fine, but you are slowing things down by using a regular expression.
Just test the array elements for equality with the value you are looking for
if (grep $_ eq $rel1, @test) {
:
}
or even better, if the values are always numeric, then use numeric equality
if (grep $_ == $rel1, @test) {
:
}
Finally, if you have to do this a lot in your program then the data should be
stored as hash keys, when you will be able to write simply
if ($test{$rel1}) {
:
}
HTH,
Rob
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/