Because I can\'t find a convenient way to check if $str
is in @array
, I\'m trying to make one myself, but it is not working.
I guess it is
You could use a prototype, but those are kind of brittle. I would pass in a reference to @f
as the first argument, like this:
use 5.010;
use strict;
use warnings;
sub ifin
{
my ($array,$str)=@_;
for my $i (@$array)
{
if ($i eq $str)
{
return True
}
}
return False
}
my @f= (1,2,3,4);
my $k=1;
print ifin(\@f,$k);
For a long list, you avoid making a copy of every list element as well.