19 lines
487 B
Text
19 lines
487 B
Text
local s = {1, 2, 2, 3, 4, 4, 5}
|
|
|
|
-- There is no output as 'prev' is created anew each time
|
|
-- around the loop and set implicitly to nil.
|
|
for i = 1, #s do
|
|
local curr = s[i]
|
|
local prev
|
|
if i > 1 and curr == prev then print(i) end
|
|
prev = curr
|
|
end
|
|
|
|
-- Now 'prev' is created only once and reassigned
|
|
-- each time around the loop producing the desired output.
|
|
local prev
|
|
for i = 1, #s do
|
|
local curr = s[i]
|
|
if i > 1 and curr == prev then print(i) end
|
|
prev = curr
|
|
end
|