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

Question about tables?

Asked by 9 years ago

I have this script where I choose a random number, then transfer it to a letter using this table. Should I be using the [1] = 'A' or the 'N'?

_G.alphabet = {
    [1] = 'A',
    [2] = 'B',
    [3] = 'C',
    [4] = 'D',
    [5] = 'E',
    [6] = 'F',
    [7] = 'G',
    [8] = 'H',
    [9] = 'I',
    [10] = 'J',
    [11] = 'K',
    [12] = 'L',
    [13] = 'M',
    'N',
    'O',
    'P',
    'Q',
    'R',
    'S',
    'T',
    'U',
    'V',
    'W',
    'X',
    'Y',
    'Z'
}

1 answer

Log in to vote
1
Answered by
M39a9am3R 3210 Moderation Voter Community Moderator
9 years ago

I would go with the N and below since you're just going after random letters. Unless a index value is defined like with A-M, then the index value will go from 1 to 2 to 3, etc.

How you can have the table spit out a random letter would be a table like below;

_G.alphabet = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'}

And then with that, you can use the math.random() function to randomly select the letter in this table, unless the table has pre-defined index values (["Test"] = true).

To have it randomly select a value you can do;

--The script should go TableNameHere[Within The Brackets Is Where The Index Value Goes]
--And # can help with tables to find out how many values are in the list.
print(_G.alphabet[math.random(1,#_G.alphabet)])

So I'll break this down a little bit more

print() --How else will you know the letter
print(_G.alphabet) --Start to have it look at the table.
print(_G.alphabet[]) --Make sure you have brackets for index value.
print(_G.alphabet[math.random()]) --Get math.random ready to random select.
print(_G.alphabet[math.random(1, #_G.alphabet)]) --1 is the first value of the index, # will get the number of values in the table

So it should print out A or C or G or T or M or Z etc...

0
Thanks! CardboardRocks 215 — 9y
Ad

Answer this question