Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
2

How do you store multiple variables together groups in a a table?

Asked by
ItsMeKlc 235 Moderation Voter
8 years ago

So I'm trying to make a title generator for one of my games, and I want to essentially have a few formats for sentences, but I'm not quite sure how to go about it... I'm trying to have them grouped together in a table (For a function I'll create later), but I'm not entirely sure how to do that.

Sorry if I'm not explaining this well, post a comment if you're confused

local nouns = {"dogs" ,"cats"}
local verbs = {"run","walk"}
local adjectives = {"fast","slow"} 
local titleformats = {nouns+verbs+adjectives,nouns+verbs}-- This is where I'm not sure what to do...

2 answers

Log in to vote
2
Answered by 8 years ago
Edited 8 years ago

Hope this helps!

Hi. On line 4 you are trying to add the tables which is not possible, you can only add numbers together. Also you want to select a random value from the table? To do so you have to get a random value from the array uses array[math.random(1, #array)] the brackets indicate getting a value from the array and the random function is going to choose 1 of all items in it.

Also to "add" strings; simply concatenate them using two periods as so print("Hello" .. " " .. "World!").

local nouns = {"dogs" ,"cats"}
local verbs = {"run","walk"}
local adjectives = {"fast","slow"} 

function randomDataCell(array)
    return array[math.random(1, #array)] 
end

local chosen_noun = randomDataCell(nouns)
local chosen_verb = randomDataCell(verbs)
local chosen_adjective = randomDataCell(adjectives)

local titleformats = chosen_noun.." "..chosen_verb.." "..chosen_adjective
print(titleformats)
0
This is rather awkward to have a large number of different formats -- you have to hand-write each one BlueTaslem 18071 — 8y
Ad
Log in to vote
2
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
8 years ago
Edited 8 years ago

You can just make a list of each:

-- your set of "adjectives" really are adverbs btw
local titleFormats = {
    {nouns, verbs, adverbs},
    {nouns, verbs},
}

Given a particle "format" (a list of word-lists, e.g., {nouns, verbs, adverbs}) you can generate a particular example by pulling from the lists:

function choose(list)
    return list[math.random(#list)]
end

function randomFromFormat(format)
    local out = {}
    for i = 1, #format do
        out[i] = choose(format[i])
    end
    return table.concat(out, " ")
end

You can re-use choose to pick a random format from all formats, and then use exampleFromFormat to pick a random choice from there:

function randomTitle()
    return randomFromFormat(choose(titleFormats))
end

Cartesian products

Another interpretation is to actually expand a "format" into all of the titles of that format.

Given options from a set A and options from a set B, you can write the set of all pairs of values from A and from B as A × B (in math notation, not Lua), called the Cartesian Product.

In this case, what you want to do is pick a random element from nouns × verbs × adverbs, so an option is to just generate all of these ahead of time.

That isn't really a good idea, because if you have 1000 of each type of word, you have one-billion possible titles, and storing them all is really wasteful (you'll only ever ask for a few).

Still, it's an interesting function, and can be easily written recursively.

function cartesianProduct(firsts, ...)
    if not firsts then
        return {{}}
    end
    local out = {}
    local rests = cartesianProduct(...)
    for _, rest in pairs(rests) do
        for _, first in pairs(firsts) do
            local copy = {first}
            for i = 1, #rest do
                copy[i+1] = rest[i]
            end
            table.insert(out, copy)
        end
    end
    return out
end

for _, title in pairs(cartesianProduct(nouns, verbs, adverbs)) do
    print(table.concat(title, " "))
end

Answer this question