A note to Perl users, strings that contain numbers will not be automatically converted to numbers when used in an expression such as when trying to read numbers from a file like the one below does.
DATA.each do |line|
vals = line.split # split line, storing tokens in val
print vals[0] + vals[1], ” ”
end
If you use a file as input that contains,
2 8
9 6
1 4
You’ll end up with; 28, 96 and 14 where they were treated as strings and not numbers. Using the string#to_i method which converts the string into an integer you get the desired result.
Sample for string#to_i string to integer conversion
DATA.each do |line|
vals = line.split # split line, storing tokens in val
print vals[0].to_i + vals[1].to_i, ” ”
end
Yielding the right answers; 10 15 5
