my $count = 0; foreach (@input) { if (/DosID(.{6})/ and $count++ % 2) { $dosid = $1; } }