Modifying an element in a Ruby array at a specified index? -


i trying learn array operations in ruby having trouble modifying specified element in array.

for context, writing program produces matrix , preforms operations on said matrix. matrix defined matix(i,j,val) i number of rows, j number of columns, , val value populates each cell of matrix when instantiated.

the matrix stored in data variable created multiple 1 dimensional arrays so:

@data = array.new(i) { array.new(j) {val} } 

i trying write function set(i,j,val) sets element @ (i,j) value stored in val. attempting achieve through iteration:

_i = 0 @data.each |sub|   if _i ==     sub[j] = val   end   _i += 1 end 

the code should iterate ith row in matrix , change element in column j. unfortunately, sub[j] = val not change value. how can change value of array @ specified index j?

don't overthink this. know index need change, can access them directly chaining [] methods. don't need iteration single value:

@data[i][j] = new_value 

Comments

Popular posts from this blog

user interface - How to replace the Python logo in a Tkinter-based Python GUI app? -

objective c - Greedy NSProgressIndicator Allocation -

how to set an OCR language in Google Drive -