lua-users home
lua-l archive

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


On Sun, May 12, 2013 at 3:06 AM, Harley Laue <losinggeneration@gmail.com> wrote:
On Sat, May 11, 2013 at 5:35 AM, Alexander Gladysh <agladysh@gmail.com> wrote:
On Fri, May 10, 2013 at 7:02 PM, Harley Laue <losinggeneration@gmail.com> wrote:
> [1] > math.randomseed(os.time()+assert(tonumber(tostring({}):sub(7))))

NB: that sub(7) is not portable — tostring({}) output differs between
Lua implementations. Use gsub() to filter out numerics instead. (And
even then it is a brittle hack, relying on unspecified behaviour.)

Alexander.

I agree it's brittle and may fail unexpectedly in the future. The assert is there to catch it when it does.  That said, 5.0 (with a change in syntax[1]), 5.1 (lua & luajit), and 5.2 all return the same thing for tostring with a table as a parameter.

[1] Lua 5.0 requires it to be changed to sub(tostring({}), 7) instead of tostring({}):sub(7) I just liked the later syntax and I don't care about supporting 5.0 because it's for personal use and I don't use 5.0.

You could support all three with:
(string and string.sub or sub)(tostring({}), 7)
But an even better way would be to use pattern matching to extract the address. (Even then, you're still relying on there being an address at all...)

--
Sent from my Game Boy.