Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

find variables... is there a way
Started by guruk Jul 29 2014 01:45 PM

- - - - -
4 replies to this topic
[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

guruk

[GLOBAL: userInfoPane.html]
guruk
  • Pro
  • PipPipPipPipPipPip
  • 575 posts
  • Jedi

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 :) but ... i ask :)

 

cheers

chris

 



[TOPIC: post.html]
#2

Caleb P

[GLOBAL: userInfoPane.html]
Caleb P
  • Pro
  • PipPipPipPipPipPip
  • 989 posts
  • Jedi

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

Here's one; possibly the simplest:
local prefix = "lg_" -- What you want global variables to be found to start withlocal prefixLength = prefix:len() -- Just an optimizationfor k, v in pairs(_G) doif k:sub(1, prefixLength) == prefix then-- Do whatever; it matches the prefixendend


- C

[TOPIC: post.html]
#3

guruk

[GLOBAL: userInfoPane.html]
guruk
  • Pro
  • PipPipPipPipPipPip
  • 575 posts
  • Jedi

wohhhhhhh !!!!

 

thats sooo cool :)

 

thanks a lot ... its amazing

 

you are a genius :)

 

 

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



[TOPIC: post.html]
#4

Caleb P

[GLOBAL: userInfoPane.html]
Caleb P
  • Pro
  • PipPipPipPipPipPip
  • 989 posts
  • Jedi

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:
-- This function converts a table to a string representationlocal 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 stringfor k, v in pairs(t) dotable.insert(str, k .. " = ")local vType = type(v) -- Take the type of 'v' so we can know how to change it into a stringif vType == "string" or vType == "number" thentable.insert(str, v) -- It can be added really simplyelseif vType == "boolean" thentable.insert(str, (v == true and "true") or "false") -- Convert Boolean to stringelseif vType == "table" thentable.insert(str, tableToString(v)) -- Convert it to a table before adding itendendreturn "{" .. table.concat(str, ", ") .. "}" -- Concatenate the key/value pairs with commasend-- Now for the looplocal prefix = "lg_" -- What you want global variables to be found to start withlocal prefixLength = prefix:len() -- Just an optimizationfor k, v in pairs(_G) doif k:sub(1, prefixLength) == prefix then -- Do whatever; it matches the prefixlocal vString = v -- What we'll print outlocal vType = type(v)if vType == "string" or vType == "number" then-- Nothing to do hereelseif vType == "boolean" thenvString = (v == true and "true") or falseelseif vType == "table" thenvString = tableToString(v)endprint(k .. " : " .. vString)endend


And here's the former:
local json = require("json") -- Include the JSON librarylocal prefix = "lg_" -- What you want global variables to be found to start withlocal prefixLength = prefix:len() -- Just an optimizationfor k, v in pairs(_G) doif k:sub(1, prefixLength) == prefix then -- Do whatever; it matches the prefixlocal vString = v -- What we'll print outlocal vType = type(v)if vType == "string" or vType == "number" then-- Nothing to do hereelseif vType == "boolean" thenvString = (v == true and "true") or falseelseif vType == "table" thenvString = json.encode(v) -- Transform the table into a JSON stringendprint(k .. " : " .. vString)endend


- C

[TOPIC: post.html]
#5

guruk

[GLOBAL: userInfoPane.html]
guruk
  • Pro
  • PipPipPipPipPipPip
  • 575 posts
  • Jedi

thank you so much ! :)

 

great

 

all the best

chris




[topic_controls]
[/topic_controls]