in reply to Searching for a string within an array using regex

grep {/^$_.*/} @WORDS

Within the block of a grep, $_ is an alias for the current element of the list being inspected. So in your example, it'll be "trying", "helping" etc. A regular expression like /.../ without a target (as opposed to $string=~/.../) searches $_ by default, so with your regex, you're testing $_ against itself, which isn't what you want. (Also, while it's not wrong, /^$x.*/ can in this case be written more simply as /^$x/.)

$_ =~ m/^@WORDS/

Here, @WORDS is interpolated into the regular expression, so your regular expression actually becomes /^trying helping doing/. Also, here you'd be searching $_ for @WORDS, which is the wrong way around.

So your first approach with grep was good, the only mistake being with the $_ variable. Here's some code in which @result will contain only the element "helping":

my @WORDS = ("trying", "helping", "doing", "whelp"); my $SEARCH = "help"; my @result = grep {/^\Q$SEARCH\E/} @WORDS; print "$_\n" for @result;

(For the meaning of \Q...\E, see quotemeta.)