lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Please help a nonexpert here...

Without ipairs(), how do I code this convenient (and clear) idiom:

--

for _,v in ipairs{'lumpi','ist','mein',hund'} do
   print(v)
end

--

lumpi
ist
mein
hund

--

Visitors to Lua from other languages reasonably expect to write code like this:

--

for v in {45,56,67,78} do
  --use the next v in the table
end

--

They can't, of course, but the code at the top provides a literate and usable alternative.

Am I right that the idiom above will become:

--

local kinderbuch = {'lumpi','ist','mein',hund'}
for i =1,#kinderbuch do
  print(kinferbuch[i])
end

--

If this is the intended new idiom, I contend that it doesn't look like literate code. (Is it just me, or does the # operator make Lua code look ugly? A syntactic device chosen because it was lexically convenient, rather than linguistically suitable?)

Is there a cleaner way to rewrite my code in 5.2?