Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > How to use split on spaces AND tabs?

Reply
Thread Tools

How to use split on spaces AND tabs?

 
 
nospam
Guest
Posts: n/a
 
      07-02-2004
The following split command will perform the
split on one or more spaces. How can I perform
the split on one or more spaces AND tabs? Or
for that matter, ANY whitespace? This reads input
from a file, and you can't trust someone's text
editor to enter tabs or spaces!

my @arr = split(/ +/, $_, 2);


-Thanks

 
Reply With Quote
 
 
 
 
Jürgen Exner
Guest
Posts: n/a
 
      07-02-2004
nospam wrote:

Hi nospam

> The following split command will perform the
> split on one or more spaces.


The first argument to split() is a good old plain regular expression. To
find out more about regular expression please see The Fine Manual: "perldoc
perlre" or for an introduction "perldoc perlretut"

> How can I perform
> the split on one or more spaces AND tabs?


Just use a character class instead of the single space
/[ \t]+/
Details see The Fine Manual

> Or
> for that matter, ANY whitespace?


Did you Read The Fine Manual for the features you are using? Just use "\s""
\s Match a whitespace character

[...]
> my @arr = split(/ +/, $_, 2);


jue


 
Reply With Quote
 
 
 
 
Joe Smith
Guest
Posts: n/a
 
      07-03-2004
nospam wrote:

> How can I perform
> the split on one or more spaces AND tabs?


RTFM. It's mentioned where split() is documented.

> my @arr = split(/ +/, $_, 2);


my @arr = split ' ',$_,2;
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to use String.split to split a mixed encoding string(partencoded in gbk, part encoded in utf-8) Stanley Xu Ruby 2 03-23-2011 02:06 PM
XSD to allow A-Z 0-9 and spaces, but not leading/trailing spaces johkar XML 2 12-10-2009 09:24 AM
Re: How to trim a String trailing spaces, but not leading spaces? Roedy Green Java 3 09-14-2008 02:10 AM
Re: How to trim a String trailing spaces, but not leading spaces? John B. Matthews Java 4 09-12-2008 05:28 AM
How to use regex to split a sentence with different spaces? www Java 2 01-30-2007 09:49 PM



Advertisments