Manipulating Multi-Dimensional Array

T

Tyler Knappe

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 = 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]]
 
J

John W Higgins

[Note: parts of this message were removed to make it a legal post.]

Morning Tyler,

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 = 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 = []
for j = 0..23 do
calendar[j] = j
end
end

John
 
T

Tyler Knappe

John said:
Morning Tyler,

On Tue, Jul 7, 2009 at 10:51 AM, Tyler Knappe


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

John


D'oh! Thanks John.
 
B

Bertram Scharpf

Hi,
Am Mittwoch, 08. Jul 2009, 02:51:16 +0900 schrieb Tyler Knappe:
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 = 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top