LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 10-22-2008, 07:58 PM   #1
sharky
Member
 
Registered: Oct 2002
Posts: 404

Rep: Reputation: 37
perl split whitespace


If I split a string that has lots of white space between strings I get some fields in the resulting array that are spaces.

For example:

Code:
#!/usr/bin/perl

$S1 = " a b c   d   e f";
@A1 = split (/ /,$S1);
print "$A1[0]\n";
print "$A1[1]\n";
print "$A1[2]\n";
print "$A1[3]\n";
print "$A1[4]\n";
print "$A1[5]\n";
this returns the following:

a
b
c


d

I can edit the file to merge the whitespace but I'm wondering if there's a way to do this with split. I would also like the leading space to be ignored.
 
Old 10-22-2008, 08:50 PM   #2
gilead
Senior Member
 
Registered: Dec 2005
Location: Brisbane, Australia
Distribution: Slackware64 14.0
Posts: 4,123

Rep: Reputation: 162Reputation: 162
I'm not at a box where I can test it, but does the following work? The + means one or more spaces:
Code:
#!/usr/bin/perl

$S1 = " a b c   d   e f";
@A1 = split (/ +/,$S1);
print "$A1[0]\n";
print "$A1[1]\n";
print "$A1[2]\n";
print "$A1[3]\n";
print "$A1[4]\n";
print "$A1[5]\n";
 
Old 10-22-2008, 09:30 PM   #3
sharky
Member
 
Registered: Oct 2002
Posts: 404

Original Poster
Rep: Reputation: 37
Quote:
Originally Posted by gilead View Post
I'm not at a box where I can test it, but does the following work? The + means one or more spaces:
Code:
#!/usr/bin/perl

$S1 = " a b c   d   e f";
@A1 = split (/ +/,$S1);
print "$A1[0]\n";
print "$A1[1]\n";
print "$A1[2]\n";
print "$A1[3]\n";
print "$A1[4]\n";
print "$A1[5]\n";
Cool, that did it.

I added this to get rid of the leading white space.

$S1 =~ s/^\s+//;

Thanks for the help.
 
Old 10-22-2008, 10:22 PM   #4
gilead
Senior Member
 
Registered: Dec 2005
Location: Brisbane, Australia
Distribution: Slackware64 14.0
Posts: 4,123

Rep: Reputation: 162Reputation: 162
No problem - I'm glad it's working
 
Old 10-23-2008, 08:13 AM   #5
Telemachos
Member
 
Registered: May 2007
Distribution: Debian
Posts: 754

Rep: Reputation: 59
It's a small thing, but I prefer to use \s+ rather than / +/ always, because it helps me to see more quickly that I am looking for whitespace. Also /\s+/ and / +/ don't match exactly the same way.

Code:
#!/usr/bin/perl
use strict;
use warnings;

my $string = "a  b\tc    d     e  f ";
my @array1 = split /\s+/, $string;
my @array2 = split / +/, $string;

print "\nSplit on \\s+\n";
foreach my $item (@array1) {
  print "$item spaces?\n";
}

print "Split on / +/\n";
foreach my $item (@array2) {
  print "$item spaces?\n";
}
Output:
Code:
telemachus ~ $ ./test 
Split on \s+
a spaces?
b spaces?
c spaces?
d spaces?
e spaces?
f spaces?

Split on / +/
a spaces?
b       c spaces?
d spaces?
e spaces?
f spaces?
 
Old 10-23-2008, 11:47 AM   #6
sharky
Member
 
Registered: Oct 2002
Posts: 404

Original Poster
Rep: Reputation: 37
Telemachos, thanks for the tip.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Using split in perl xjlittle Programming 9 06-21-2007 08:13 AM
Perl split ShaqDiesel Programming 5 08-17-2006 09:02 PM
awk messing up trying to split a unicode line by whitespace hedpe Programming 1 08-05-2006 12:10 PM
split command in perl not splitting / linuxmandrake Programming 1 11-03-2005 04:28 PM
Perl Help (possible split) fooforon Programming 2 02-19-2004 06:53 AM


All times are GMT -5. The time now is 12:17 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration