while (match('a')) { print "- $_\n"; # $_ is undef all the time } #### while (my $entry = match('a')) { print "- $entry\n"; # prints the correct results } #### 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"; }