Velocity Reviews > Perl > Split problem.

# Split problem.

Richard S Beckett
Guest
Posts: n/a

 11-05-2003
Guys,

I have a line of text like this " 5 6 0 7 176
0". i.e. 9 spaces before the 5.

When I try to place the numbers into an array with split (my @temp = split
(/\s*/, \$input), I get a blank element at the start. What am I doing
wrong?

Thanks.
=-=-=-=-=-=-=-=-=-=-
use strict;
use warnings;
my \$line = " 5 6 0 7 176 0";
my @array = split (/\s*/, \$line);
print "Starting...\n";
foreach (0..\$#array) {
print "element \$_ = \$array[\$_]\n";
}
print "Finished.\n";
=-=-=-=-=-=-=-=-=-=-
OUTPUT:
Starting...
element 0 =
element 1 = 5
element 2 = 6
element 3 = 0
element 4 = 7
element 5 = 1
element 6 = 7
element 7 = 6
element 8 = 0
Finished.
--
R.
GPLRank +79.699

Andreas Kahari
Guest
Posts: n/a

 11-05-2003
In article <bob4es\$t49\$(E-Mail Removed)>, Richard S Beckett wrote:
> Guys,
>
> I have a line of text like this " 5 6 0 7 176
> 0". i.e. 9 spaces before the 5.
>
> When I try to place the numbers into an array with split (my @temp = split
> (/\s*/, \$input), I get a blank element at the start. What am I doing
> wrong?

If a list is separated by colons like this:

1:2:3:4:5

then we can agree on that there are five items in the list.

:1:2:3:4:5

Perl believes that the first element is an empty element since
we've separatedt it from the second element, which is '1'.

The "solution" would be to remove all flanking separators:

\$list =~ s/^:*//;
\$list =~ s/:*\$//;

and then split it

@arr = split /:/, \$list;

This applies to your choise of seperator as well.

--
Andreas Kähäri

John J. Trammell
Guest
Posts: n/a

 11-05-2003
On Wed, 5 Nov 2003 15:18:27 -0000, Richard S Beckett wrote:
> Guys,
>
> I have a line of text like this " 5 6 0 7 176
> 0". i.e. 9 spaces before the 5.
>
> When I try to place the numbers into an array with split (my @temp = split
> (/\s*/, \$input), I get a blank element at the start. What am I doing
> wrong?

If you "perldoc -f split", you'll see:

As a special case, specifying a PATTERN of space (' ') will split on
white space just as "split" with no arguments does.

So try:

my @tmp = split(" ",\$input);

Richard S Beckett
Guest
Posts: n/a

 11-05-2003
> > Guys,

> > I have a line of text like this " 5 6 0 7 176

> > 0". i.e. 9 spaces before the 5.

> > When I try to place the numbers into an array with split (my @temp =

split

> > (/\s*/, \$input), I get a blank element at the start. What am I doing

> > wrong?

> If a list is separated by colons like this:

> 1:2:3:4:5

> then we can agree on that there are five items in the list.

> :1:2:3:4:5

> Perl believes that the first element is an empty element since

> we've separatedt it from the second element, which is '1'.

> The "solution" would be to remove all flanking separators:

> \$list =~ s/^:*//;

> \$list =~ s/:*\$//;

> and then split it

> @arr = split /:/, \$list;

> This applies to your choise of seperator as well.

Aaaaaaah!

Thanks Andreas.

R.

Richard S Beckett
Guest
Posts: n/a

 11-05-2003
> If you "perldoc -f split", you'll see:
> As a special case, specifying a PATTERN of space (' ') will split on
> white space just as "split" with no arguments does.
> So try:
> my @tmp = split(" ",\$input);

I did read it, but didn't really understand it. Sorry.

So you're saying that:
my @tmp = split(" ",\$input);

is the equivalent of:
\$input =~ s/^\s*//;
my @tmp = split (/\s+/, \$input);

?

The thing that threw me was that there are different numbers of spaces
between the numbers in \$input, so I didn't think that would work.

Thanks.

R.

Jeff 'japhy' Pinyan
Guest
Posts: n/a

 11-05-2003
[posted & mailed]

On Wed, 5 Nov 2003, Richard S Beckett wrote:

>I have a line of text like this " 5 6 0 7 176
>0". i.e. 9 spaces before the 5.
>
>When I try to place the numbers into an array with split (my @temp = split
>(/\s*/, \$input), I get a blank element at the start. What am I doing
>wrong?

The thing you're doing wrong doesn't have to do with the leading empty
field. You should be using split(/\s+/, \$input), because \s* will match
ZERO spaces (which is why '176' is split into 1, 7, and 6).

The split function WILL return empty leading fields if they exist, so
using split(/\s+/, \$input) will still return an empty leading field.

However, there's a special case: split(' ', \$input). This will split on
multiple spaces, AND ignore a leading empty field.

--
Jeff Pinyan RPI Acacia Brother #734 2003 Rush Chairman
"And I vos head of Gestapo for ten | Michael Palin (as Heinrich Bimmler)
years. Ah! Five years! Nein! No! | in: The North Minehead Bye-Election
Oh. Was NOT head of Gestapo AT ALL!" | (Monty Python's Flying Circus)

John J. Trammell
Guest
Posts: n/a

 11-05-2003
On Wed, 5 Nov 2003 16:48:12 -0000, Richard S Beckett wrote:
> [Trammell wrote:]
>> If you "perldoc -f split", you'll see:
>> As a special case, specifying a PATTERN of space (' ') will split on
>> white space just as "split" with no arguments does.
>> So try:
>> my @tmp = split(" ",\$input);

>
> I did read it, but didn't really understand it. Sorry.
>
> So you're saying that:
> my @tmp = split(" ",\$input);
>
> is the equivalent of:
> \$input =~ s/^\s*//;
> my @tmp = split (/\s+/, \$input);
>
> ?

Again from perldoc -f split:

A "split" on "/\s+/" is like a "split(' ')" except that any leading
whitespace produces a null first field. A "split" with no arguments
really does a "split(' ', \$_)" internally.

ph33r my 1337 perldoc sk33lz!

John W. Krahn
Guest
Posts: n/a

 11-09-2003
Richard S Beckett wrote:
>
> I have a line of text like this " 5 6 0 7 176
> 0". i.e. 9 spaces before the 5.
>
> When I try to place the numbers into an array with split (my @temp = split
> (/\s*/, \$input), I get a blank element at the start. What am I doing
> wrong?

Use the match operator to get the data you want instead of using split
to remove the data you don't want.

my @array = \$line =~ /\d/g;

Or if you really want ( 176 ) instead of ( 1, 7, 6 ) then:

my @array = \$line =~ /\d+/g;

John
--
use Perl;
program
fulfillment