String split into an array

M

Michael Myers

Hi,

is it possible to do something like that in ruby?

(Perl-Code)
($run_number, $iteration, $day, @nodes) = split /\s+/;

String looks like the first 3 numbers for run, iteration and day
followed by n numbers for each node.
I want to bind those n nodes to an array @nodes.

run_number, iteration, day, nodes = line.split(" ")
does not work, nodes only get the first node and the rest of the line is
ignored.
 
R

Rimantas Liubertas

is it possible to do something like that in ruby?
(Perl-Code)
($run_number, $iteration, $day, @nodes) = split /\s+/;

String looks like the first 3 numbers for run, iteration and day
followed by n numbers for each node.
I want to bind those n nodes to an array @nodes.

run_number, iteration, day, nodes = line.split(" ")
does not work, nodes only get the first node and the rest of the line is
ignored.
The fix is easy:

run_number, iteration, day, *nodes = line.split(" ")

(note "*" in front of nodes).


Regards,
Rimantas
 
M

Michael Myers

Okay, found the solution:

just write *nodes like

run_number, iteration, day, *nodes = line.split(" ")
 

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,989
Messages
2,570,207
Members
46,782
Latest member
ThomasGex

Latest Threads

Top