How I fill a two dimensional array with two nested loops?

Hi,
I’m trying to fill a two dimensional array with a nested for loops.
I would like to get the following:

((“0,0”,“0,1”,“0,2”),(“1,0”,“1,1”,“1,2”))

My attemp is this:

#{
  let i = 0
  let j = 0
  let col-num = 3
  let row-num = 2
  let values = while i < row-num {
    (while j < col-num{
      let v = str(i) + "," + str(j)
      (v,)
      j = j + 1
    },)
    i = i + 1
  }
  values
}

And i got:

image

But I don’t find any documentation about it.
Thanks a lot!

Found it!

#{
  let i = 0
  let col-num = 3
  let row-num = 2
  let values = while i < row-num {
    let j = 0
    (while j < col-num{
      let v = str(i) + "," + str(j)
      (v,)
      j = j + 1
    },)
    i = i + 1
  }
  values
}

Hi, you don’t even need loops to do this, using .map() on arrays is pretty powerful:

#{
	let col-num = 3
	let row-num = 2
   	let rows = array.range(row-num)
	let cols = array.range(col-num)

	rows.map(r => cols.map(c => str(r) + "," + str(c)))
}
1 Like