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

Question about tables?

Asked by 10 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'?

01_G.alphabet = {
02    [1] = 'A',
03    [2] = 'B',
04    [3] = 'C',
05    [4] = 'D',
06    [5] = 'E',
07    [6] = 'F',
08    [7] = 'G',
09    [8] = 'H',
10    [9] = 'I',
11    [10] = 'J',
12    [11] = 'K',
13    [12] = 'L',
14    [13] = 'M',
15    'N',
View all 28 lines...

1 answer

Log in to vote
1
Answered by
M39a9am3R 3210 Moderation Voter Community Moderator
10 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;

1_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;

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

So I'll break this down a little bit more

1print() --How else will you know the letter
2print(_G.alphabet) --Start to have it look at the table.
3print(_G.alphabet[]) --Make sure you have brackets for index value.
4print(_G.alphabet[math.random()]) --Get math.random ready to random select.
5print(_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 — 10y
Ad

Answer this question