Split problem.

  • Thread starter Richard S Beckett
  • Start date
R

Richard S Beckett

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.
 
A

Andreas Kahari

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.
What about this list then:

: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.
 
J

John J. Trammell

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);
 
R

Richard S Beckett

Guys,
split



If a list is separated by colons like this:

then we can agree on that there are five items in the list.
What about this list then:

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.
 
R

Richard S Beckett

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.
 
J

Jeff 'japhy' Pinyan

[posted & mailed]

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.
 
J

John J. Trammell

[attributions fixed]
[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!
 
J

John W. Krahn

Richard said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top