my $goal = 'over';
my @array = qw(the quick fox jumped over the lazy dog);
foreach my $item (@array) {
next unless $item =~ /$goal/;
print $item;
}
####
my $goal = 'over';
my @array = qw(the quick fox jumped over the lazy dog);
for (@array) {
next unless /$goal/;
print $_;
last
}
####
my $goal = 'over';
my @array = qw(the quick fox jumped over the lazy dog);
my $found_item;
foreach my $item (@array) {
next unless $item =~ /$goal/;
$found_item = $item;
last;
}
if ($found_item) {
print $found_item;
# do other stuff
}
####
my $goal = 'over';
my @array = qw(the quick fox jumped over the lazy dog);
my $found;
foreach my $item (@array) {
if ($item =~ /$goal/) {
$found = 1; # in case we need it later
print $item;
# do other stuff
last;
}
}