You want to use the split() function. It's aptly named. It takes a pattern to split a string on, and the string to split.
@chunks = split /\s+/, "thing with spaces in it";
If you don't know regexes yet, you'll probably want to study up on them.
You could also use a special case of this function (as explained in its documentation):
@chunks = split ' ', $string_to_split;
Please read the docs for more information.
_____________________________________________________
Jeff [japhy]Pinyan:
Perl,
regex,
and perl
hacker, who'd like a job (NYC-area)
s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;
| [reply] [d/l] [select] |
- Don't call your sub 'x'. That's the name of a perl operator.
- You can use magical split to divide your text on whitespace:
sub words {
split " ", $_[0];
}
That works by splitting the first argument given to the sub. In a more complex sub, I would have shifted the first argument into a temporary variable.
| [reply] [d/l] |
An alternative is to use the @array = $str =~ /(pattern)/g idiom...
sub foo {
my $str = shift;
my @text = $str =~ /(\S+)/g;
...
}
# call as
foo("This is a test");
| [reply] [d/l] [select] |