#Lets say this is an array:
my @foo=("Dog", "Cat", "Dog and Cat", "Dog, Cat and Dog");
#parse this from a loop
for (my $i=0;$i<=@foo;$i++) {
print "in \$foo[$i] found a Dog\n" if ($foo[$i]=~/Dog/);
print "in \$foo[$i] found a Cat\n" if ($foo[$i]=~/Cat/);
print "in \$foo[$i] found more Dogs\n" if ($foo[$i]=~/Dog.*Dog/);
}
This will print :
in $foo[0] found a Dog
in $foo[1] found a Cat
in $foo[2] found a Dog
in $foo[2] found a Cat
in $foo[3] found a Dog
in $foo[3] found a Cat
in $foo[3] found more Dogs
___________
Panos
my @a = qw/cat dog cat_and_dog/;
my $search = qr/Cat/i; # I'm searching for 'cat'
# find all elements with 'cat' in them:
my @b = grep {/$search/} @a;
# find indices of elements with 'cat' in them:
my ($i, @c) = (0);
map { push @c, $i if /$search/; $i++ } @a;
# find all parts in array that match search term (this
# is why I made the regex case-insensitive....!)
my @d;
map { push @d, $1 if /($search)/; } @a;
print<<_END_;
Results
=======
Variable\tContents
\@b\t\t@b
\@c\t\t@c
\@d\t\t@d
_END_ Results I get (remember, stringified arrays have their elements separate by spaces):
Results
=======
Variable Contents
@b cat cat_and_dog
@c 0 2
@d cat cat
Has to be one of these, right?!