ruby - IO.readlines to multidimensional array -
the goal of program read in file has grid of numbers delimited white space. process in program need multidimensional array. attempted by:
arr = io.readlines("example.txt") arr.map { |string| string.chomp.split.map { |sub_str| sub_str.to_i } }
this code, when ran, return original array, strings , '\n'. have been able desired effect by:
arr = io.readlines("example.txt") int = array.new arr.each { |string| int << string.chomp.split.map { |sub_str| sub_str.to_i } }
i prefer mutate original array rather created new one. how can map array desired results? new ruby , enjoying oop'ness. help.
just reassign original array
arr = arr.map { |string| string.chomp.split.map { |sub_str| sub_str.to_i } }
or use destructive map!
arr.map! { |string| string.chomp.split.map { |sub_str| sub_str.to_i } }
Comments
Post a Comment