From: Dhanabal Date: 2013-07-19T16:30:22+09:00 Subject: Re: Iteration anomalu Hi, In second iteration the value we are getting in 'i' is [0..3] Certainly it will print all the value of a and then it prints the new line... it works properly. try this..... for i in [0..a.length] do print i print a[i], "\n" end 123456789 print a[0..2] if you want the same result, try this it will print new line for i in 0...a.length do print a[i], "\n" end 123 456 789 Cheers, Dhanabal On Thursday 18 July 2013 02:13 AM, Andrew D. wrote: > Surely this is a FAQ but I can't figure out how I would search for an > explanation for this behavior. In short, this code > > a = [[1,2,3], [4,5,6], [7,8,9]] > > for x in a do > print x, "\n" > end > > for i in [0..a.length] do > print a[i], "\n" > end > > yields this result: > > > 123 > 456 > 789 > 123456789 > > Why does the second iteration evaluate all the prints before printing > the newline? > > I have some puzzling variations on this too, but I am sure that the > answer to this will clear them up. Thanks! > > FWIW, the reason I even consider the second iteration style, old-school > that it is, is because I want to do a double-nested iteration through a, > in this way: > > for i in [0..a.length] do > for j in [i+1..a.length] do > # etc... processing lower-left-hand triangle of possibilities... >