From: Chris Hulan Date: 2006-07-21T04:34:35+09:00 Subject: Re: recursive method and references Sci000 Lem. wrote: ... > def tyra(arr, pos2, tmp2, resul, res=0, i=0) > (puts "#####"; resul.push(res); return) if i>=13 > (0..3).each do |y| > pos=pos2.dup; tmp=tmp2.dup ... > next if pos[tmp[0]][tmp[1]]==1 > pos[tmp[0]][tmp[1]]=1 > tyra(arr, pos.dup, tmp.dup, resul, res+arr[tmp[0]][tmp[1]], i+1) > print "*", pos2, "\n", "_", pos, "\n" > end > end > > arr, pos, tmp, resul = [], [], [], [] > arr.push([32,80,19,98,01,90,14,85]) > arr.push([66,22,73,52,72,57,83,31]) > arr.push([30,84,41,73,16,74,45,92]) > arr.push([77,06,70,24,00,28,67,11]) > arr.push([32,99,44,81,27,75,42,98]) > arr.push([68,21,72,56,59,42,75,17]) > arr.push([34,87,19,92,05,99,27,88]) > > arr[0].length.times{tmp.push(0)} > arr.length.times{pos.push(tmp.dup)} > tmp2=[3,4] > > print "#", pos, "\n" > tyra(arr, pos.dup, tmp2.dup, resul) > print "#", pos, "\n" > > > The output is: > #00000000000000000000000000000000000000000000000000000000 > *00000000000000000000000000001000000000000000000000000000 > _00000000000000000000000000001000000000000000000000000000 > #00000000000000000000000000001000000000000000000000000000 > > > The lines marked with a "#" does not equal each other as with the test > function… > What goes wrong, where’s the bug? Your pos array contains references to other arrays, and pos.dup is a shallow copy, thus pos2 contains refs to the same sub-arrays as pos. Any changes made via access to pos2 affects the same sub-arrays as viewed from pos. Clear as mud? 9^) Cheers Chris -- Posted via http://www.ruby-forum.com/.