my $seen;
for ( grep $seen ||= ($_ eq "valueC"), @array ) {
...
}
Run Code Online (Sandbox Code Playgroud)
my $seen;
for ( @array ) {
$seen++ if /valueC/;
next unless $seen;
...
}
Run Code Online (Sandbox Code Playgroud)
但这$seen有点笨拙.该触发器操作看起来更整洁IMO:
for ( @array ) {
next unless /^valueC$/ .. /\0/;
# or /^valueC$/ .. '' !~ /^$;
# or $_ eq 'valueC' .. /\0/;
...
}
Run Code Online (Sandbox Code Playgroud)
或者简单地说(建立在ikegami的建议上):
for ( grep { /^valueC$/ .. /(*FAIL)/ } @array ) { ... }
Run Code Online (Sandbox Code Playgroud)
我想你还需要检查数组中是否存在"valueC".
希望这可以帮助.
use strict;
use warnings;
use List::Util qw(first);
my @array = qw(valueA valueB valueC valueD);
my $starting_element = 'valueC';
# make sure that the starting element exist inside the array
# first search for the first occurrence of the $stating_element
# dies if not found
my $starting_index = first { $array[$_] eq $starting_element } 0 .. $#array
or die "element \"$starting_element\" does not exist inside the array";
# your loop
for my $index ($starting_index .. $#array) {
print $array[$index]."\n";
}
Run Code Online (Sandbox Code Playgroud)