in reply to Extract a specific number of words from a string

Hi, $str is a variable, maybe you meant substr?

Try this:

$ perl -Mstrict -wE 'my $str = "mary had a little lamb"; say for ($st +r =~ /(\S+)/g)[0..2]'
( See perlrequick, perlretut )

Hope this helps!


The way forward always starts with a minimal test.