string - Return multiple variables from logical statements -
this code explode string list of each individual character without using tables:
function explode(s) if#s==1 return s end return s:sub(1,1),explode(s:sub(2)) end
where #s==#({explode(s)})
in attempt make code shorter, wanted this:
function explode(s) return#s>1 and(s:sub(1,1),explode(s:sub(2)))or s end
but won't work because '[condition] , [result value] b or [alternative] c' doesn't call having multiple results or alternatives. there other way return same result using 1 statement?
here idea:
function explode(s) return s:match(("(.)"):rep(s:len())) end
note not efficient.
Comments
Post a Comment