Parsing one-liners

K

Kevin Olbrich

The problem:
read in a string containing an arbitrary number of comma separated
numbers and convert it to an array of numbers.

It seems like this could be a one-liner, but the best I can get is two...

w=input_string.split(/,\s*/)
w.each_index {|n| w[n]=w[n].to_f}

Anyone know a one-liner for this?

_Kevin
 
E

email55555

If your numbers are *true* floats (for example 123.0 instead of 123),
then you could try this:

w = eval("[#{input_string}]")
 

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,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top