Manipulating Multi-Dimensional Array

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
irb(main):198:2> end
irb(main):199:1> end
=> 0…6
irb(main):200:0> calendar
=> [[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,
19, 20, 21, 22, 23], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22, 23], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23], [0, 1, 2, 3, 4, 5,
6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23], [0,
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
21, 22, 23], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
17, 18, 19, 20, 21, 22, 23], [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23]]
irb(main):201:0> calendar[0][0]
=> 0
irb(main):202:0> calendar[0][1]
=> 1
irb(main):203:0> calendar[0]
=> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,
19, 20, 21, 22, 23]
irb(main):204:0> calendar[0][0] = ‘True’
=> “True”
irb(main):205:0> calendar[0]
=> [“True”, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
18, 19, 20, 21, 22, 23]
irb(main):206:0> calendar[1]
=> [“True”, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
18, 19, 20, 21, 22, 23]
irb(main):207:0> calendar[1][0]
=> “True”
irb(main):208:0> calendar[0] = ‘True’
=> “True”
irb(main):209:0> calendar[0]
=> “True”
irb(main):210:0> calendar[1]
=> [“True”, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
18, 19, 20, 21, 22, 23]
irb(main):211:0> calendar
=> [“True”, [“True”, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
16, 17, 18, 19, 20, 21, 22, 23], [“True”, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23], [“True”, 1, 2, 3,
4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22,
23], [“True”, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
18, 19, 20, 21, 22, 23], [“True”, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23], [“True”, 1, 2, 3, 4, 5, 6,
7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23]]

Morning Tyler,

On Tue, Jul 7, 2009 at 10:51 AM, Tyler K.
[email protected]wrote:

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

John W Higgins wrote:

Morning Tyler,

On Tue, Jul 7, 2009 at 10:51 AM, Tyler K.
[email protected]wrote:

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

John

D’oh! Thanks John.

At 2009-07-07 02:12PM, “John W Higgins” wrote:

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

How about
calendar = 7.times.collect {(0…23).to_a}

Hi,
Am Mittwoch, 08. Jul 2009, 02:51:16 +0900 schrieb Tyler K.:

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
irb(main):198:2> end
irb(main):199:1> end

The answer has been given; here’s a more rudimentary example of
this pitfall:

a = b = []
b.push :x
a #=> [ :x]

Say instead of that:

a, b = [], []

Yes, I like to express it in code lines.

Bertram

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs