Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I loop over an array from the first occurrence of an element with a specific value using perl?

Tags:

arrays

loops

perl

I have an array like ("valueA", "valueB", "valueC", "valueD") etc. I want to loop over the values of the array starting from (for example) the first instance of "valueC". Everything in the array before the first instance of the value "valueC" should be ignored; so in this case only "valueC" and "valueD" would be handled by the loop.

I can just put a conditional inside my loop, but is there a neater way to express the idea using perl?

like image 520
lexicalscope Avatar asked Nov 28 '22 09:11

lexicalscope


1 Answers

my $seen;
for ( grep $seen ||= ($_ eq "valueC"), @array ) {
    ...
}
like image 154
choroba Avatar answered Dec 16 '22 09:12

choroba