[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

unable to reason

Daun Jaun

4/12/2005 7:13:00 AM

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


4 Answers

Eric Hodel

4/12/2005 7:41:00 AM

0


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

> 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 :)

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

Csaba Henk

4/12/2005 7:43:00 AM

0

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

Meditate over the following koan:

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

Csaba

Daun Jaun

4/12/2005 3:52:00 PM

0

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


Eric Hodel

4/12/2005 4:39:00 PM

0

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

oops, you need some ()s, sorry

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

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