Unable to reason

statArr=[]
for i in 0...3
    statArr[i]=[]
    for j in 0...3
  statArr[i][j]=[]
      for k in 0...2
    statArr[i][j][k]=0
  end
    end
end

p statArr
statArr[0][2][1]+=3
statArr[1][0][0]+=2
statArr[2][2][1]+=1
p statArr

statArr=Array.new(3,Array.new(3,Array.new(2,0)))
p statArr
statArr[0][2][1]+=3
statArr[1][0][0]+=2
statArr[2][2][1]+=1
p statArr

what is going on in the above code why is there the discrepancy in the output
is it a bug or paraller processing feature

please figure it out and explain

statArr=
for i in 0...3
    statArr[i]=
    for j in 0...3
  statArr[i][j]=
      for k in 0...2
    statArr[i][j][k]=0
  end
    end
end

p statArr
statArr[0][2][1]+=3
statArr[1][0][0]+=2
statArr[2][2][1]+=1
p statArr

statArr=Array.new(3,Array.new(3,Array.new(2,0)))

Here you asked Ruby to create an Array containing three copies of the same Array, not three different Arrays.

try stat_arr = Array.new 3 { Array.new 3 { Array.new 2, 0 }}

p statArr
statArr[0][2][1]+=3
statArr[1][0][0]+=2
statArr[2][2][1]+=1
p statArr

what is going on in the above code why is there the discrepancy in the output
is it a bug or paraller processing feature

It is your bug :slight_smile:

PGP.sig (194 Bytes)

···

On 12 Apr 2005, at 00:12, Daun Jaun wrote:

--
Eric Hodel - drbrain@segment7.net - http://segment7.net
FEC2 57F1 D465 EB15 5D6E 7C11 332A 551C 796C 9F04

Meditate over the following koan:

  Array.new(3,"x").map{|x| x.object_id}.uniq.size

Csaba

···

On 2005-04-12, Daun Jaun <compsci.isi@gmail.com> wrote:

what is going on in the above code why is there the discrepancy in the output
is it a bug or paraller processing feature

please figure it out and explain

i didnot get it
trying stat_arr = Array.new 3 { Array.new 3 { Array.new 2, 0 }}
gave syntax error

oops, you need some ()s, sorry

stat_arr = Array.new(3) { Array.new(3) { Array.new 2, 0 }}

PGP.sig (194 Bytes)

···

On 12 Apr 2005, at 08:51, Daun Jaun wrote:

i didnot get it
trying stat_arr = Array.new 3 { Array.new 3 { Array.new 2, 0 }}
gave syntax error

--
Eric Hodel - drbrain@segment7.net - http://segment7.net
FEC2 57F1 D465 EB15 5D6E 7C11 332A 551C 796C 9F04