lua-users home
lua-l archive

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


> If you're happy with that, then tostring() will provide you with the
> identity of tables and userdata (although be careful of metatables
> overriding that).  I don't think there's a way of obtaining the hash (or
> address) of a string from plain Lua, but the remaining data types can
> all be hashed using algorithms written in Lua.

Since version 5.4, we can use the option "%p" in 'string.format' to
get the address of anything in Lua that has an address, including
strings:

  Lua 5.4.2  Copyright (C) 1994-2020 Lua.org, PUC-Rio
  > string.format("%p", "hi")
  0x5626ae7b4150
  > string.format("%p", "hello")
  0x5626ae7b5bf0
  > string.format("%p", print)
  0x5626ad9b2709
  > print(print)
  function: 0x5626ad9b2709

However, long strings can have multiple copies inside Lua, so equal
strings can have different addresses.

-- Roberto