find variables... is there a way

hi, 

may sound strange, but maybe someone has an idea.

i have lots of global variables like

_G.username

_G.testfile

_G.lg_test1

_G.lg_runtime

_G.lg_info

_G.hallo

ok… no i would like to give the users to chance to change to content

for all variables that start with _G.lg_   like _G.lg_test1

i know i could write all _G.lg_ files down in a extra list and offer a inputline to change.

but its over 400 ! of that _G.lg_ variables and now I MAY save some time

if one of u has an idea how to internally by code I could discover all variables like that

and put it in an array for example.

i expect there is no solution and i have to do the manual way :slight_smile: but … i ask :slight_smile:

cheers

chris

There are several of ways you could do that, actually.

Here’s one; possibly the simplest:
[lua]
local prefix = “lg_” – What you want global variables to be found to start with
local prefixLength = prefix:len() – Just an optimization

for k, v in pairs(_G) do
if k:sub(1, prefixLength) == prefix then
– Do whatever; it matches the prefix
end
end
[/lua]

  • C

wohhhhhhh !!!

thats sooo cool :slight_smile:

thanks a lot … its amazing

you are a genius :slight_smile:

so now i use

local prefix = “lg_”                        – What you want global variables to be found to start with

local prefixLength = prefix:len()    – Just an optimization

for k, v in pairs(_G) do

    if k:sub(1, prefixLength) == prefix  then       – Do whatever; it matches the prefix

       print (k … " : " … v)

    end

end

so i print k (the variable name) and its value (v)

but a few Values (v) are tables like

_G.lg_months_s =   {“Jan”, “Feb”, “Mar”, “Apr”, “Mai”, “Jun”, “Jul”,“Aug”,“Sep”,“Okt”,“Nov”,“Dez”}

there it crash, how would i extend that function so it also shows the variables containing tables correct?

thanks again and

greets

chris

What you want now is a table to string function. You can use JSON; it outputs quite readable tables, or you can make your own quick table-to-string function.

Here’s the latter approach:
[lua]
– This function converts a table to a string representation
local function tableToString(t)
local str = {} – It’s a table because then we’ll concatenate it together and it’s faster than doing it to a string

for k, v in pairs(t) do
table.insert(str, k … " = ")

local vType = type(v) – Take the type of ‘v’ so we can know how to change it into a string
if vType == “string” or vType == “number” then
table.insert(str, v) – It can be added really simply
elseif vType == “boolean” then
table.insert(str, (v == true and “true”) or “false”) – Convert Boolean to string
elseif vType == “table” then
table.insert(str, tableToString(v)) – Convert it to a table before adding it
end
end

return “{” … table.concat(str, ", ") … “}” – Concatenate the key/value pairs with commas
end

– Now for the loop
local prefix = “lg_” – What you want global variables to be found to start with
local prefixLength = prefix:len() – Just an optimization
for k, v in pairs(_G) do
if k:sub(1, prefixLength) == prefix then – Do whatever; it matches the prefix
local vString = v – What we’ll print out
local vType = type(v)
if vType == “string” or vType == “number” then
– Nothing to do here
elseif vType == “boolean” then
vString = (v == true and “true”) or false
elseif vType == “table” then
vString = tableToString(v)
end
print(k … " : " … vString)
end
end
[/lua]

And here’s the former:
[lua]
local json = require(“json”) – Include the JSON library

local prefix = “lg_” – What you want global variables to be found to start with
local prefixLength = prefix:len() – Just an optimization
for k, v in pairs(_G) do
if k:sub(1, prefixLength) == prefix then – Do whatever; it matches the prefix
local vString = v – What we’ll print out
local vType = type(v)
if vType == “string” or vType == “number” then
– Nothing to do here
elseif vType == “boolean” then
vString = (v == true and “true”) or false
elseif vType == “table” then
vString = json.encode(v) – Transform the table into a JSON string
end
print(k … " : " … vString)
end
end
[/lua]

  • C

thank you so much ! :slight_smile:

great

all the best

chris

There are several of ways you could do that, actually.

Here’s one; possibly the simplest:
[lua]
local prefix = “lg_” – What you want global variables to be found to start with
local prefixLength = prefix:len() – Just an optimization

for k, v in pairs(_G) do
if k:sub(1, prefixLength) == prefix then
– Do whatever; it matches the prefix
end
end
[/lua]

  • C

wohhhhhhh !!!

thats sooo cool :slight_smile:

thanks a lot … its amazing

you are a genius :slight_smile:

so now i use

local prefix = “lg_”                        – What you want global variables to be found to start with

local prefixLength = prefix:len()    – Just an optimization

for k, v in pairs(_G) do

    if k:sub(1, prefixLength) == prefix  then       – Do whatever; it matches the prefix

       print (k … " : " … v)

    end

end

so i print k (the variable name) and its value (v)

but a few Values (v) are tables like

_G.lg_months_s =   {“Jan”, “Feb”, “Mar”, “Apr”, “Mai”, “Jun”, “Jul”,“Aug”,“Sep”,“Okt”,“Nov”,“Dez”}

there it crash, how would i extend that function so it also shows the variables containing tables correct?

thanks again and

greets

chris

What you want now is a table to string function. You can use JSON; it outputs quite readable tables, or you can make your own quick table-to-string function.

Here’s the latter approach:
[lua]
– This function converts a table to a string representation
local function tableToString(t)
local str = {} – It’s a table because then we’ll concatenate it together and it’s faster than doing it to a string

for k, v in pairs(t) do
table.insert(str, k … " = ")

local vType = type(v) – Take the type of ‘v’ so we can know how to change it into a string
if vType == “string” or vType == “number” then
table.insert(str, v) – It can be added really simply
elseif vType == “boolean” then
table.insert(str, (v == true and “true”) or “false”) – Convert Boolean to string
elseif vType == “table” then
table.insert(str, tableToString(v)) – Convert it to a table before adding it
end
end

return “{” … table.concat(str, ", ") … “}” – Concatenate the key/value pairs with commas
end

– Now for the loop
local prefix = “lg_” – What you want global variables to be found to start with
local prefixLength = prefix:len() – Just an optimization
for k, v in pairs(_G) do
if k:sub(1, prefixLength) == prefix then – Do whatever; it matches the prefix
local vString = v – What we’ll print out
local vType = type(v)
if vType == “string” or vType == “number” then
– Nothing to do here
elseif vType == “boolean” then
vString = (v == true and “true”) or false
elseif vType == “table” then
vString = tableToString(v)
end
print(k … " : " … vString)
end
end
[/lua]

And here’s the former:
[lua]
local json = require(“json”) – Include the JSON library

local prefix = “lg_” – What you want global variables to be found to start with
local prefixLength = prefix:len() – Just an optimization
for k, v in pairs(_G) do
if k:sub(1, prefixLength) == prefix then – Do whatever; it matches the prefix
local vString = v – What we’ll print out
local vType = type(v)
if vType == “string” or vType == “number” then
– Nothing to do here
elseif vType == “boolean” then
vString = (v == true and “true”) or false
elseif vType == “table” then
vString = json.encode(v) – Transform the table into a JSON string
end
print(k … " : " … vString)
end
end
[/lua]

  • C

thank you so much ! :slight_smile:

great

all the best

chris