Ad
Log in to vote
0

Table String To Variable?

Asked by 4 years ago
Edited 4 years ago

3rd time editing this since it's not 'clear enough'... HOW... DO... YOU... CONVERT... A... STRING... TO... A... FORM... THAT... IT... CAN... BE... USED... AS... A... VARIABLE...

local table = {
one = "wasupe"
}
table[one] = 100
0
Horrible Question. Word it properly. Zafirua 1348 — 4y
0
i think you might be looking for "tostring" but this is too vague of a question NinjaboySC 11 — 4y
0
To get the "String" from the table, loop through the table, check for the type of the element. When you find string, just do table[v + 1] Zafirua 1348 — 4y
0
epic green271 635 — 4y

1 answer

Log in to vote
5
Answered by
Zafirua 1348 Badge of Merit Moderation Voter
4 years ago
Edited 4 years ago

Posting it in case your mind cannot comprehend my writing

local tab = {1, 2, 3, "Stupid", "Next"};

for i = 1, #tab -1 do
    local current = tab[i];
    if (type(current) == "string") then
        local nextVal = tab[i + 1];
    end
end

--Revoked Code

From my understanding, there is no way to "name" actual variable like that. My example show above, it isn't a variable to begin with. So, I don't know.

Long story short, you are doing something that you wouldn't even need to do. Post your scenario. Why do you need to do this. Then maybe we can help you with an alternative.

Another Edit.

If you wanted user to type in like Tab[Money] in chat and want to return the money's value, you could do this.

local var = { -- Container for all the "Chat Methods"
    Tab = { -- "Tab will be the Table that will hold the properties
        Money = 100; -- If you wanted more, you can simply add more. Like
         Age  = 2;
    }, 

   Give = {
    AddMoney = 100;
   }    

-- And so on....
}

local input = io.read("*line"); -- If you are doing in ROBLOX, there isn't io. But you can get the message from the chat. I won't do that here. Too lazy. 

local function decode (input)
    local p1 = input:find("%[");    -- Find the First occurrence of [
    local prefix = input:sub(1, p1 -1);  -- Will be the container you want to go into. 
    local nxt = input:sub(p1 + 1, #input - 1); -- Will be the field's value you want to fetch
    print(prefix, nxt)
    return var[prefix][nxt] -- Returns the fields value. 
end;

print(decode(input));

if you type Tab[Money] it will go into Var and find Tab. Then it will find the Money and return its value.

You can probably make it better. But this is the gist of it. Good luck, whatever you are doing.

Ad

Answer this question