in reply to Using the 'if' statement...
The second snippet is still wrong. Read up on "|" and "||" in perlop. You should be using the latter.
On to your question:
my @countries = ( "India", "America", "Germany", "Austria", ); if ( grep { $_ eq $country } @countries ) { print "True"; }
Or a version that stops as soon as it finds a match:
my @countries = ( "India", "America", "Germany", "Austria", ); for (@countries) { if ($_ eq $country) { print "True"; last; } }
Or use a hash to avoid looping except during initialization:
my @countries = ( "India", "America", "Germany", "Austria", ); my %countries = map { $_ => 1 } @countries; if ($countries{$country}) { print "True"; }
|
|---|