From: CC Chen Date: 2011-07-15T11:05:27+09:00 Subject: Re: array overwrite question CC Chen wrote in post #1010408: > Joey Zhou wrote in post #1010405: >> bb and cc are a same object, you change cc, bb will also be changed. >> >> you should modify the 3rd line to: >> >> cc = bb.dup > > > > If on multi-dimension array? > > I try .dup it cant work!! > > > Thanks, aa2 = [[2,3],[4,5],[6,7]] bb2 = [[11,22],[33,44],[6,7]] cc2 = aa2.dup i = 0 while i< 3 j = 0 while j < 2 if (aa2[i][j]== bb2[i][j]) cc2[i][j] = 88 end j+=1 end i+=1 end print aa2,"\n" print bb2,"\n" print cc2 The output are: aa2 = [[2,3],[4,5],[88,88]] bb2 = [[11,22],[33,44],[6,7]] cc2 = [[2,3],[4,5],[88,88]] I want its output: aa2 = [[2,3],[4,5],[6,7]] bb2 = [[11,22],[33,44],[6,7]] cc2 = [[2,3],[4,5],[88,88]] The .dup seems just can work on one dimension array. How to solve the problems on multi-dimension array? Thanks, -- Posted via http://www.ruby-forum.com/.