A simple question using SPLIT

A

Aman

Hello everyone,

I am new to perl and I am trying to split a line but can't seem to get
it to work. Basically, the line is made up of multiple whitespaces.
An example of a line is as follows and I want to split it into
variables.

1 1 808748 1081461 0 0 0 1 3 0 744 1190 850 0 1 98 1

I tried using the following

($r,$b,$avm)=split(/\s/,$line);

to split the first 3 rows but it does not work. Any ideas on how to
accomplish this ? Ideally I would like to split each col. into a
variable. Any help will be appreciated. Thank

Aman
 
T

Toni Erdmann

Aman said:
Hello everyone,

I am new to perl and I am trying to split a line but can't seem to get
it to work. Basically, the line is made up of multiple whitespaces.
An example of a line is as follows and I want to split it into
variables.

1 1 808748 1081461 0 0 0 1 3 0 744 1190 850 0 1 98 1

I tried using the following

($r,$b,$avm)=split(/\s/,$line);

to split the first 3 rows but it does not work. Any ideas on how to
accomplish this ? Ideally I would like to split each col. into a
variable. Any help will be appreciated. Thanks

Aman

($r,$b,$avm) = split( /\s+/, $line );
^

Toni
 
J

Joe Smith

Aman said:
Hello everyone,

I am new to perl and I am trying to split a line but can't seem to get
it to work. Basically, the line is made up of multiple whitespaces.
An example of a line is as follows and I want to split it into
variables.

1 1 808748 1081461 0 0 0 1 3 0 744 1190 850 0 1 98 1

I tried using the following

($r,$b,$avm)=split(/\s/,$line);

to split the first 3 rows but it does not work.

You need to re-read the docs on split().

Take note of the section in `perldoc -f split` where it says:
As a special case, specifying a PATTERN of space (' ') will...

-Joe
 
D

Damian James

...
1 1 808748 1081461 0 0 0 1 3 0 744 1190 850 0 1 98 1

I tried using the following

($r,$b,$avm)=split(/\s/,$line);

to split the first 3 rows but it does not work. Any ideas on how to
accomplish this ? Ideally I would like to split each col. into a
variable. Any help will be appreciated. Thanks

That's because you're matching a single whitespace character. As others,
have pointed out,

($r,$b,$avm)=split(/\s+/,$line);

is probably what you meant. Note, however, that to match only the first 3 items,
you'd probably want:

($r,$b,$avm,$rest)=split(/\s+/,$line, 4);

I believe the confusion here is because this is the (often unspoken) default
for split, so if you had done:

$_ = '1 1 808748 1081461 0 0 0 1 3 0 744 1190 850 0 1 98 1';
($r,$b,$avm)=split;

That would give the same result, but without the limit as above.

--Damian.
 

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,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top