in reply to How can I place each "word" of a string in a varible if I don't know the length of the string?

Like this methinks:
@words = $string =~ m/(\w+)/g;
This will place each matching item into the array @words, the matching criteria is a greedy comparison on \w that gives a word character ie. not a space,tab,comma...etc.

The g at the the end of the regular expression is "global" so it will keep doing the match regardless of the string length. Regular Expressions are a very good place to start whenever you want to do tricksy stuff with a string.

Good sources of learning are:

I hope this helps ;^)

--
Brother Frankus.
  • Comment on Regular expressions to store matching patterns in an array.

Replies are listed 'Best First'.
Re: Regular expressions to store matching patterns in an array.
by ketema (Scribe) on Jun 22, 2005 at 20:58 UTC
    Is it possible to do the opposite? Make an assignment of one scalar if a match is found in the elements of an array? I'm sure this syntax is wrong, but something like:
    @terms = ("Word1","Word2","Word3"); $match = $1 if($lineFromSomeTextFile =~ /(^@terms)/);
    I know the regular expression part is wrong, but is there some advanced Monk way of accomplishing this? What about grep? can that somehow be incorporated into the regex?