anon has asked for the wisdom of the Perl Monks concerning the following question:

I need to place each "word" of string in a unique variable, but I don't know how many "words" there are. Normally I would use split, but uncertain how to use it here.

Help?

2005-09-16 jdporter moved from Snippets Section.

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

Replies are listed 'Best First'.
Re: How can I place each "word" of a string in a varible if I don't know the length of the string?
by Trimbach (Curate) on Dec 04, 2000 at 09:54 UTC
    You ought to use split if you don't know the number of "words" (and by words I'm assuming you mean space-delimited text) ahead of time. Heck, even if you do you should use split. For example:
    # Yes, I know how many "words" there are. ($word_one, $word_two, $word_three) = split " ", $sentence; # No, I don't know how many words there are. @words = split " ", $sentence;
    The second example gives you an array filled with all the words in the sentence, in order. Now, if your question is how to put all the words into individually named scalar variables the answer is you can't. I mean, logically, how can you put n number of words into n number of scalars when n is unknown? You can't. That's why God invented the array, so you don't have to know. @words will be exactly as long as the number of words in your sentence, and you can get to each one whenver you wish, and have your nasty little way with them.

    Gary Blackburn
    Trained Killer

      Why don't you just split the @words array again, this time in a loop, stating that each variable in the array becomes its own ($word1 , $word2 , etc.) scalar ? The 1, 2, 3, you can create with a simple increment. Does that contribute to the solution ?
Regular expressions to store matching patterns in an array.
by frankus (Priest) on Mar 15, 2001 at 16:15 UTC
    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:

    • perldoc perlre
    • O'Reilly Owl Book - Mastering Regular Expressions.

    I hope this helps ;^)

    --
    
    Brother Frankus.
      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?
Re: How can I place each "word" of a string in a varible if I don't know the length of the string?
by EvanK (Chaplain) on Jan 10, 2001 at 23:34 UTC
    how about something along the lines of: @words = qw($string); Just a suggestion, mind you...no need to attack me if I'm wrong.

    ______________________________________________
    If the world didn't suck, we'd all fly off.
           -A friend of mine

      You might of wanted to test that statement out:
      @words = qw($string); puts the value '$string' into $words[0]
      A suggestion. If you think there is a good chance that you may be wrong, try testing before posting.

      Incidentally your suggestion of qw($string) is wrong.