lua-users home
lua-l archive

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


Table.unpack can receive the table length as an optional argument. You can get this length from select("#", ...) or from table.pack (it adds a ".n" field to the table it returns).

Also consider if parseNumberWhimsically could return false instead of nil.

-- Hugo


Em 7 de novembro de 2023 00:56:31 BRT, "Родион Горковенко" <rodiongork@gmail.com> escreveu:
Hi Friends!

Regard the case - I'm redefining io.read(...) - for example to allow
3-rd party code run in web-page (with Lua compiled to JS), requesting
user to provide input data via pop-ups. To properly mock the original
function behavior, I would like it to return "nil" sometimes (say upon
reading number when
no digits were provided).

io.read = function(...)
res = {}
for _, typ in ipairs(table.pack(...)) do
val = someCodeWhichCallsPrompt()
if typ == '*n' then val = parseNumberWhimsically(val) end
table.insert(res, val)
end
return table.unpack(res)
end

The difficulty is that if "parseNumberWhimsically" may return "nil",
it won't be inserted into the table. I don't know whether "table.pack"
users some complicated magic for this or on contrary I'm missing
something obvious. Thanks in advance for any hints!

sincerely yours,
Rodion