Morning Tyler,

On Tue, Jul 7, 2009 at 10:51 AM, Tyler Knappe <tyler.knappe / gmail.com>wrote:

> I'm attempting to modify elements within a multi-dimensional array.
> However, syntax like calendar[0][0] = 'True' results in the entire
> calendar[0..6][0] portion of the multi-dimensional array to be set to
> 'True'.  Can anyone explain to me why this is occurring?
>
> irb(main):192:0> calendar = []
> => []
> irb(main):193:0> cal = []
> => []
> irb(main):194:0> for i in 0..6 do
> irb(main):195:1* for j in 0..23 do
> irb(main):196:2* cal[j] = j
> irb(main):197:2> calendar[i] = cal


You set each member of calendar to the same array. That is why when you
change the value of calendar[0][0] you are actually modifying cal[0] which
means you are modifying calendar[0][0], calendar[1][0], and so on because
calendar[0] == calendar[1] == cal

You can try

for i = 0..6 do
  calendar[i] = []
  for j = 0..23 do
    calendar[i][j] = j
  end
end

John