ruby - Array of integers from file -
i trying grab large numbers file, store them array, , add them up.
nums = array.new x, total=0, 0 file = file.open("inputfile.txt", 'r') while !file.eof? nums[x] = file.readline x+=1 end while x>0 x-=1 total += nums[x] end puts total when code executes, error saying:
string can't coerced fixnum (typeerror) i checked make sure each array element has right data in it. think numbers considered strings when stored. if that's case, how change array integers can total them up?
just modify
nums[x] = file.readline to
nums[x] = file.readline.to_i cheers!
Comments
Post a Comment