LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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, 06:58 PM   #1
sharky
Member
 
Registered: Oct 2002
Posts: 569

Rep: Reputation: 84
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, 07:50 PM   #2
gilead
Senior Member
 
Registered: Dec 2005
Location: Brisbane, Australia
Distribution: Slackware64 14.0
Posts: 4,141

Rep: Reputation: 168Reputation: 168
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, 08:30 PM   #3
sharky
Member
 
Registered: Oct 2002
Posts: 569

Original Poster
Rep: Reputation: 84
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, 09:22 PM   #4
gilead
Senior Member
 
Registered: Dec 2005
Location: Brisbane, Australia
Distribution: Slackware64 14.0
Posts: 4,141

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

Rep: Reputation: 60
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, 10:47 AM   #6
sharky
Member
 
Registered: Oct 2002
Posts: 569

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


Reply



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 07:13 AM
Perl split ShaqDiesel Programming 5 08-17-2006 08:02 PM
awk messing up trying to split a unicode line by whitespace hedpe Programming 1 08-05-2006 11:10 AM
split command in perl not splitting / linuxmandrake Programming 1 11-03-2005 03:28 PM
Perl Help (possible split) fooforon Programming 2 02-19-2004 05:53 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:54 PM.

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
Open Source Consulting | Domain Registration