unable to reason

D

Daun Jaun

statArr=[]
for i in 0...3
statArr=[]
for j in 0...3
statArr[j]=[]
for k in 0...2
statArr[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
 
E

Eric Hodel

--Apple-Mail-23--307016069
Content-Transfer-Encoding: 7bit
Content-Type: text/plain; charset=US-ASCII; format=flowed


statArr=[]
for i in 0...3
statArr=[]
for j in 0...3
statArr[j]=[]
for k in 0...2
statArr[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 - (e-mail address removed) - http://segment7.net
FEC2 57F1 D465 EB15 5D6E 7C11 332A 551C 796C 9F04

--Apple-Mail-23--307016069
content-type: application/pgp-signature; x-mac-type=70674453;
name=PGP.sig
content-description: This is a digitally signed message part
content-disposition: inline; filename=PGP.sig
content-transfer-encoding: 7bit

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (Darwin)

iEYEARECAAYFAkJbe4YACgkQMypVHHlsnwSVTACfRi+r8BfjLVFWTx8Jjap9qLHf
IHsAnjhgvWvmne0nybjduZabUSbPYdgD
=dDrN
-----END PGP SIGNATURE-----

--Apple-Mail-23--307016069--
 
C

Csaba Henk

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
 
D

Daun Jaun

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

Eric Hodel

--Apple-Mail-24--274755446
Content-Transfer-Encoding: 7bit
Content-Type: text/plain; charset=US-ASCII; format=flowed

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 - (e-mail address removed) - http://segment7.net
FEC2 57F1 D465 EB15 5D6E 7C11 332A 551C 796C 9F04

--Apple-Mail-24--274755446
content-type: application/pgp-signature; x-mac-type=70674453;
name=PGP.sig
content-description: This is a digitally signed message part
content-disposition: inline; filename=PGP.sig
content-transfer-encoding: 7bit

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (Darwin)

iEYEARECAAYFAkJb+Y8ACgkQMypVHHlsnwSdggCfXhi9RpqKjWe5KMSgQ7/cM1wQ
KP4An10abfYWAVqnOzKPCUD1w4V1f5l9
=r8Lo
-----END PGP SIGNATURE-----

--Apple-Mail-24--274755446--
 

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
474,170
Messages
2,570,924
Members
47,464
Latest member
Bobbylenly

Latest Threads

Top