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

1local nouns = {"dogs" ,"cats"}
2local verbs = {"run","walk"}
3local adjectives = {"fast","slow"}
4local 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!").

01local nouns = {"dogs" ,"cats"}
02local verbs = {"run","walk"}
03local adjectives = {"fast","slow"}
04 
05function randomDataCell(array)
06    return array[math.random(1, #array)]
07end
08 
09local chosen_noun = randomDataCell(nouns)
10local chosen_verb = randomDataCell(verbs)
11local chosen_adjective = randomDataCell(adjectives)
12 
13local titleformats = chosen_noun.." "..chosen_verb.." "..chosen_adjective
14print(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:

1-- your set of "adjectives" really are adverbs btw
2local titleFormats = {
3    {nouns, verbs, adverbs},
4    {nouns, verbs},
5}

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:

01function choose(list)
02    return list[math.random(#list)]
03end
04 
05function randomFromFormat(format)
06    local out = {}
07    for i = 1, #format do
08        out[i] = choose(format[i])
09    end
10    return table.concat(out, " ")
11end

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

1function randomTitle()
2    return randomFromFormat(choose(titleFormats))
3end

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.

01function cartesianProduct(firsts, ...)
02    if not firsts then
03        return {{}}
04    end
05    local out = {}
06    local rests = cartesianProduct(...)
07    for _, rest in pairs(rests) do
08        for _, first in pairs(firsts) do
09            local copy = {first}
10            for i = 1, #rest do
11                copy[i+1] = rest[i]
12            end
13            table.insert(out, copy)
14        end
15    end
View all 21 lines...

Answer this question