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

Re: the best way to separate a string into words

by jeffa (Bishop)
on Jan 19, 2009 at 19:16 UTC ( [id://737372]=note: print w/replies, xml ) Need Help??


in reply to the best way to separate a string into words

my @array = split( /\s+/, $string );

jeffa

L-LL-L--L-LL-L--L-LL-L--
-R--R-RR-R--R-RR-R--R-RR
B--B--B--B--B--B--B--B--
H---H---H---H---H---H---
(the triplet paradiddle with high-hat)

Replies are listed 'Best First'.
Re^2: the best way to separate a string into words
by repellent (Priest) on Jan 19, 2009 at 19:48 UTC
    It'd probably be best to split using the magical ' '.
    my $string = " a b c "; # note leading/trailing whitespaces my @array1 = split /\s+/, $string; # returns '', 'a', 'b', 'c' (4 item +s) my @array2 = split ' ', $string; # returns 'a', 'b', 'c' (3 item +s)
Re^2: the best way to separate a string into words
by derby (Abbot) on Jan 19, 2009 at 19:31 UTC

    I'd go with the \W+ ... but I guess it all depends on how you define a word.

    -derby

      I might too, but the OP defined words as "separated by space characters"

      jeffa

      L-LL-L--L-LL-L--L-LL-L--
      -R--R-RR-R--R-RR-R--R-RR
      B--B--B--B--B--B--B--B--
      H---H---H---H---H---H---
      (the triplet paradiddle with high-hat)
      

        Doh! note to self ... read node fully

        -derby

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://737372]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (3)
As of 2024-04-25 23:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found