Need help getting tables to work.

Started by ComputerCrafter, Apr 13, 2024, 03:07 AM

Previous topic - Next topic

Which idea should I use to check if a character is matched?

LOTS of If Statements
0 (0%)
If Statements with Functions
0 (0%)
Use custom Table API
1 (100%)
Use different methods than above
0 (0%)
Use Variables instead of Tables
0 (0%)

Total Members Voted: 1

I was trying to make a character guessing game...but it didn't go so well.
The goal was to have a table-config system where you can add custom characters and prompts.

I couldn't make it work because the wiki site was down, and finding a way to check the table prompts was too hard.

I hope some more well-versed coders here can help me solve my problem! :)
I've already tried using variables, and using if statements (that's going to be very hard because of the sheer number of characters), and they didn't work.

Likewise, I added a poll if you want to vote for different ideas on how to solve these problems.

martsadas

local letters = {'a', 'c', 'e', 'f', 'm', 'o', 'p', 'r', 't', 'u'}

local function guess(letter)
    -- not not converts it to a bool

    return not not table.concat(letters):find(letter)
end
No signature set.

QuickMuffin8782

Quote from: ComputerCrafter on Apr 13, 2024, 03:07 AM-snip-
Tables are sorted as lists in JSON, but with a different format to it.
Here's one example. With whitespaces it can look like this:
{
  ["key"] = "value",
  [123] = 1246409.249,
  [0x1] = 0xff,
}
And another without whitespaces.
{1,"2",0x3}
Basically tables help out as databases for your programs, such as info they can retrieve. However if you want it to look through the list based on the function it's trying to execute, you can use the for loop like this:
for key, value in pairs(tableVariableName) do
    -- functionality here
end
Table lengths can be done via the # symbol like #tableVariableName for example. When this method is done, it returns the length of the table, which is highly better than indexing it to get it's length.

There are more documentation of functions with the table API built-in to Lua, so don't feel afraid to hop on this website to look at the documentation. To find the version, just simply open the lua program on your CC computer and type this and press enter. Your shell should look similar to this.
CraftOS 1.9
> lua
Interactive Lua prompt.
Call exit() to exit.
lua> _VERSION
"Lua 5.1"
lua> _
This is what it looked like on my end, however I'm not sure about yours since your copy is different. Hope you get successful with the fun program you're making!
"The Blue Blur" - Working on Net Star OS, a new frontier for Red Star OS 3.0
Gitlab - Join my official discord! - SS Discord