Split text in a line to 3 variables

F

felad

Hi

I have a file with several lines with blank as separator that look like
that:
AAA BBB CCC
BSB NKN OOO yyy uuu
QQQ PPP LKL
BSB NKN OOO yyy

How can I put the first and second words in a line into 2 variables
and the rest of the line into a third variable
 
G

Gunnar Hjalmarsson

felad said:
I have a file with several lines with blank as separator that look like
that:
AAA BBB CCC
BSB NKN OOO yyy uuu
QQQ PPP LKL
BSB NKN OOO yyy

How can I put the first and second words in a line into 2 variables
and the rest of the line into a third variable

my ($first, $second, $third) = split ' ', $_, 3;

See "perldoc -f split".
 
I

it_says_BALLS_on_your forehead

felad said:
Hi

I have a file with several lines with blank as separator that look like
that:
AAA BBB CCC
BSB NKN OOO yyy uuu
QQQ PPP LKL
BSB NKN OOO yyy

How can I put the first and second words in a line into 2 variables
and the rest of the line into a third variable

you should really look at the perldocs...they explain exactly how to do
this. but if you're *really* too lazy to look...

split /PATTERN/, EXPR, LIMIT

my ($field1, $field2, $theRest) = split ' ', $string, 3;
 
T

Tad McClellan

felad said:
Subject: Split text in a line to 3 variables
^^^^^
^^^^^

You have asked a SAQ (Self Answering Question)

perldoc -f split

I have a file with several lines with blank as separator
How can I put the first and second words in a line into 2 variables
and the rest of the line into a third variable

my($var1, $var2, $rest) = split /\s+/, $somestring, 3;
 

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,774
Messages
2,569,596
Members
45,128
Latest member
ElwoodPhil
Top