How can one get a 2-dimensional array in row 3 and the length of the column?
My array goes like this:
payroll = array.new [payroll count] [2] When we are the length of a 1-dimensional array we do
array.length How about a 2-dimensional array? I am thinking of doing something like this:
payroll = array.new [payroll count] [2] i in 0..payroll.row.length - 1 for j In 0. .. $ payroll [i] [j] end end I just want to know the right way. Pls help ...
A two-dimensional array is just an array of arrays, so only Payroll length to get the height and salary [0]. To get width (length of all rows), to get the length width. It is that your loop looks like using the idea: i for 0..payroll.length - 1 in 0 for jar .. parole [i]. Length - 1 puts a payroll [i] [j] end end But an easy way to loop through the array is using an iterator method. Here I'll change the for loops to each .with_index (use every_with_index if your ruby each .with_index ) : payroll.each.with_index do | Line, i | Do Row.each.with_index | Cell, J. Say payroll [i] [j] end end And now I make it even simpler because I'm assuming that you do not really need access to the indexes, just array Personal elements in:
payroll.each do | Row | Line.Hold | Room | Puts cell end and
Comments
Post a Comment