tags:

views:

354

answers:

3

I want to do this in Perl:

>> "foo bar baz".scan /(\w+)/
=> [["foo"], ["bar"], ["baz"]]

Any suggestions?

+12  A: 

This does essentially the same thing.

my @elem = "foo bar baz" =~ /(\w+)/g

You can also set the "default scalar" variable $_.

$_ = "foo bar baz";
my @elem = /(\w+)/g;

See perldoc perlre for more information.


If you only want to use that string as an array, you could use qw().

my @elem = qw"foo bar baz";

See perldoc perlop ​ ​( Quote and Quote-like Operators ).

Brad Gilbert
IMHO, explicit assignment to `$_` merely to use one of Perl's *implicit* `$_` idioms awkwardly misses the point of that idiomatic shorthand.
pilcrow
I know, I want to expand on that part of the answer, to show where it would be useful.
Brad Gilbert
+3  A: 

Also, split, e.g.,

my $x = "foo bar baz";
my @elem = split(' ', $x);

OR

my @elem = split(/\w+/, $x);

etc.

Chris Cleeland
I think you mean "`split /(\w+)/` ..."
Brad Gilbert
`split ' ', $x` is unsatisfactory if there may be `\W` characters (other than `\s`) in the string. `split /(\w+)/, $x` will give you `('', 'foo', ' ', 'bar', ' ', 'baz')` making this a pointless exercise.
Sinan Ünür
Sinan is right, I looked at using split and it isn't right for this purpose. I just want an array of elements matching the regex.
klochner
You're correct that split(' ',...) is unsatisfactory in more complex cases. It will work for the trivial case presented.Thanks for catching the gaff on split(/\w+/). I'm the complement of the OP, where I don't know Ruby but know perl, and misunderstood scan's functionality.
Chris Cleeland
split /\W+/, 'foo bar baz' gives 'foo', 'bar', 'baz' as requested
larelogio
but is it always going to be that easy to invert the regular expression for use in split()?
klochner
I think that, based on a better understanding of ruby's scan() method, that perl's split is not the appropriate choice. split and scan are opposites, and I shouldn't have brought up split in the first place.
Chris Cleeland
+1  A: 

They key perl concept is scalar vs list context. Assigning an expression to an array forces list context, as does something like a while loop.

Thus, the equivalent of the block form of String#scan is to use the regexp with a while loop:

while ("foo bar baz" =~ /(\w+)/) {
    my $word = $1;
    do_something_with($word);
}
robc