Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Answer: How do I return the first n words of a string?

by Fastolfe (Vicar)
on Oct 26, 2000 at 18:59 UTC ( #38628=categorized answer: print w/replies, xml ) Need Help??

Q&A > strings > How do I return the first n words of a string? - Answer contributed by Fastolfe

The first two things that come to my mind:
$_ = "some string with multiple words"; @first_three = (split)[0..2]; # or ($first_three) = /^\s*(\S+\s+\S+\s+\S+)/;
You can probably adapt either of these to your purposes.

Log In?
Username:
Password:

What's my password?
Create A New User
Chatterbox?
and the daffodils sway...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (5)
As of 2017-11-18 03:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    In order to be able to say "I know Perl", you must have:













    Results (277 votes). Check out past polls.

    Notices?