length() and looping
- Posted by jessedavis Mar 10, 2017
- 1462 views
4.0.5 (362497032f33, 2012-10-11)
Can anyone explain the behavior of the following code? What am I missing?
include std/console.e sequence data = {} for i = 1 to 5 do data = append(data,64 + i) end for for i = 1 to 5 do printf(1,"%d %s\n",{i,data[i]}) end for any_key() --stops program and awaits <cr> for i = 1 to length(data)/2 do --length(data)/2 = 2, so... -- for i = 1 to 2 do printf(1,"%d %s\n",{i,data[i]}) end for any_key() printf(1,"First time: length(data) = %d\n\n",length(data)) for i = length(data)/2 + 1 to length(data) do --for i = 3 to 5 do printf(1,"%d %s\n",{i,data[i]}) end for printf(1,"\nSecond time: length(data) = %d\n",length(data)) any_key() puts(1,"Now for the finale!\n") for i = 1 to 5 do printf(1,"%d %s\n",{i,data[i]}) end for puts(1, "I seem to have misplaced the letter E!\n") any_key()
1 A 2 B 3 C 4 D 5 E 1 A 2 B First time: length(data) = 5 3 C 4 D Second time: length(data) = 5 Now for the finale! 1 A 2 B 3 C 4 D 5 E I seem to have misplaced the letter E!