dani24
dani24

Reputation: 2288

Lua - get table hex identifier

I want to know how to get the table hex id. I know that doing:

local some_var = {}
print (some_var)

the result is (for instance):

table: 0x21581c0

I want the hex without the table: string. I know that maybe some of you suggest me to make a regular expression (or something similar) to remove those chars, but I want to avoid that, and just get the 0x21581c0

Thanks

Upvotes: 3

Views: 4328

Answers (4)

LeoLuz
LeoLuz

Reputation: 528

This is simpler and works for all types that are associated with pointers:

local function getId(t)
  return string.format("%p", t)
end

print("string:", getId("hi"))
print("table:", getId({}))
print("userdata:", getId(io.stdin))
print("function:", getId(print))
print("number:", getId(1))
print("boolean:", getId(false))
print("nil:", getId(nil))

Result:

string: 0x0109f04638
table: 0x0109f0a270
userdata: 0x01098076c8
function: 0x0109806018
number: NULL
boolean: NULL
nil: NULL

Upvotes: 5

Scott Bishop
Scott Bishop

Reputation: 26

The table id stated by the OP is invalid in the version of Lua I am using (5.1 in Roblox). A valid ID is length 8, not 9 as in your example. Either way, just use string.sub to get the sub-string you are after.

string.sub(tostring({}), 8)

The reason is, 'table: ' is 7 characters long, so we take from index 8 through the end of the string which returns the hex value.

Upvotes: 0

Tom Blodget
Tom Blodget

Reputation: 20812

In the standard implementation, there is the global 'print' variable that refers to a standard function that calls, through the global variable 'tostring', a standard function described here. The stanard 'tostring' function is the only way to retrieve the hexadecimal number it shows for a table.

Unfortunately, there is no configuration for either of the functions to do anything differently for all tables.

Nonetheless, there are several points for modification. You can create you own function and call that every time instead, or point either of the the global variables print or tostring to you own functions. Or, set a __tostring metamethod on each table you need tostring to return a different answer for. The advantage to this is it gets you the format you want with only one setup step. The disadvantage is that you have to set up each table.

local function simplifyTableToString(t)
   local answer = tostring(t):gsub("table: ", "", 1)
   local mt = getmetatable(t) 
   if not mt then
      mt = {}
      setmetatable(t, mt)
   end
   mt.__tostring = function() return answer end
end

local a = {}
local b = {}    
print(a, b)
simplifyTableToString(a)
print(a, b)

Upvotes: 2

Oka
Oka

Reputation: 26355

Without complex patterns, you can just search for the first space, and grab the substring of what follows.

function get_mem_addr (object)
    local str = tostring(object)    
    return str:sub(str:find(' ') + 1)
end

print(get_mem_addr({})) -- 0x109638
print(get_mem_addr(function () end)) -- 0x108cf8

This function will work with tables and functions, but expect errors if you pass it anything else.

Or you can use a little type checking:

function get_mem_addr (o)
    return tostring(o):sub(type(o):len() + 3)
end

Upvotes: 2

Related Questions