my $matchPos = 0; sub match { my ($key) = @_; while ($matchPos < @data) { $matchPos++; return $data[$matchPos - 1] if ($data[$matchPos - 1] =~ /$key/); } return undef; } while (match('a')) { print "- $_\n"; }