On 22/08/2011 08:42, timothy adigun wrote:
Hi Anant,

I want to search whether a scalar '$a' contains any one of value of an
array
'@arr'.
How it may be done?
as its not correct : print "found" if $a =~ /@arr/;

#!/usr/bin/perl
use strict;
use warnings;

  my @arr=qw(fry ring apple law);
  print "Enter the string you are searching for:";
  chomp(my $search=<STDIN>); # you may have to check, if input is not string

  grep {print "found: $_" if/\b$search\b/}@arr;

  #grep compare your search with each array element
  # using $_ and then print out if item is found.

This is a miuse of the grep operator, which is filtering @arr according
to the expression in the braces and returning a new list. But you are
throwing away that list and using only the side effects. It should be
written

  /\b$search\b/ and print "found: $_\n" for @arr;

Rob

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to