2

How would I go about compiling values from a table using a string? i.e.

NumberDef = {
[1] = 1,
[2] = 2,
[3] = 3
}

TextDef = { 
["a"] = 1,
["b"] = 2,
["c"] = 3
}

If I was for example to request "1ABC3", how would I get it to output 1 1 2 3 3?

Greatly appreciate any response.

2 Answers 2

3

Try this:

s="1ABC3z9"

t=s:gsub(".",function (x)
    local y=tonumber(x)
    if y~=nil then
        y=NumberDef[y]
    else
        y=TextDef[x:lower()]
    end
    return (y or x).." "
end)

print(t)

This may be simplified if you combine the two tables into one.

Sign up to request clarification or add additional context in comments.

Comments

2

You can access values in a lua array like so:

TableName["IndexNameOrNumber"]

Using your example:

NumberDef = {
[1] = 1,
[2] = 2,
[3] = 3
}

TextDef = { 
["a"] = 1,
["b"] = 2,
["c"] = 3
}

print(NumberDef[2])--Will print 2
print(TextDef["c"])--will print 3

If you wish to access all values of a Lua array you can loop through all values like so (similarly to a foreach in c#):

for i,v in next, TextDef do
print(i, v) 
end
--Output:
--c    3
--a    1
--b    2

So to answer your request, you would request those values like so:

print(NumberDef[1], TextDef["a"], TextDef["b"], TextDef["c"], NumberDef[3])--Will print   1 1 2 3 3

One more point, if you're interested in concatenating lua string this can be accomplished like so:

string1 = string2 .. string3

Example:

local StringValue1 = "I"
local StringValue2 = "Love"

local StringValue3 = StringValue1 .. " " .. StringValue2 .. " Memes!"
print(StringValue3) -- Will print "I Love Memes!"

UPDATE I whipped up a quick example code you could use to handle what you're looking for. This will go through the inputted string and check each of the two tables if the value you requested exists. If it does it will add it onto a string value and print at the end the final product.

local StringInput = "1abc3" -- Your request to find  
local CombineString = "" --To combine the request

NumberDef = {
[1] = 1,
[2] = 2,
[3] = 3
}

TextDef = { 
["a"] = 1,
["b"] = 2,
["c"] = 3
}

for i=1, #StringInput do --Foreach character in the string inputted do this
    local CurrentCharacter = StringInput:sub(i,i); --get the current character from the loop position
    local Num = tonumber(CurrentCharacter)--If possible convert to number

    if TextDef[CurrentCharacter] then--if it exists in text def array then combine it
        CombineString = CombineString .. TextDef[CurrentCharacter] 
    end
    if NumberDef[Num] then --if it exists in number def array then combine it
        CombineString = CombineString .. NumberDef[Num] 
    end
end

print("Combined: ", CombineString) --print the final product.

4 Comments

Hi, the real question, although I might not have worded it correctly - is there a way to go through the tables and making them print the values for "1ABC3" without having to manually getting the values as you posted?
You could combine the numeric and text table into one single array ( as lua has compatibility for mixed value arrays) So you could make a loop code to go through your string and index each value at a time, combine them and return the requested value.
So I would explode the string and go through every value in the combined table?
Thank you for your help, this seemed to solve this riddle :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.