I used an inner array (or hash) in nested blocks to save data:
p "#1 both inner_arr and outer_arr set empty outside of both loops"
outer_arr = []
inner_arr = []
i = 0
2.times{
j = 0
2.times{
inner_arr[j] = j+100+i
j += 1
}
p inner_arr
outer_arr[i] = inner_arr
i += 1
p outer_arr
}
p "======================================================"
p "#2 outer_arr set empty before both loops while inner_arr set empty inside of outer loop and outside of inner loop"
outer_arr_2 = []
i = 0
2.times{
j = 0
inner_arr_2 = []
2.times{
inner_arr_2 << j+100+i
j += 1
}
p inner_arr_2
outer_arr_2[i] = inner_arr_2
i += 1
p outer_arr_2
}
p "======================================================"
p "#3 both outer and inner hash set empty outside of both loops"
outer_hash_3 = {}
inner_hash_3 = {}
i = 0
2.times{
j = 0
2.times{
inner_hash_3 [j] = j+100+i
j += 1
}
p inner_hash_3
outer_hash_3[i] = inner_hash_3
i += 1
p outer_hash_3
}
p "======================================================"
p "#4 outer_hash set empty before both loops while inner_hash set empty inside of outer loop and outside of inner loop"
outer_hash_4 = {}
i = 0
2.times{
j = 0
inner_hash_4 = {}
2.times{
inner_hash_4[j] = j+100+i
j += 1
}
p inner_hash_4
outer_hash_4[i] = inner_hash_4
i += 1
p outer_hash_4
}
If I didn't reset the inner array (or hash) when the inner array (or hash) changed, it updated data I had saved in the outer array. If I reset the inner array (or hash), then it won't update the outer array. I don't understand why this is happening. Please help me understand it.