very basic Qs on data, files, tables, strings

Intro / Apology: I’m very much a newbie. I’ve tried looking in various lua docs for my answers, but a couple of chapters in I always go from “yup, I knew that” to “I don’t know any of those words.” Even my questions may be phrased incorrectly, and I may be using most of these words wrong. Thanks for anyone patient enough to proceed…

I’d like to write a basic Quiz app using data pairs, e.g. States and Capitals. I’d like to store various different data sets in the app (eventually allowing the user to select from a list, but for starters I’m fine with manually changing the code to point to one in particular). I’d like to store each data set in its own tab in the app.

Question-Blob 1 is:
What format should I use in the file? I’ve seen things that look like:

Items = {
“this”,
“that”,
“the other”
}

But I want pairs of items that stay related to one another, e.g. “California, Sacramento”. I’d also like each item to have one or more additional fields, so that I can store a “score” or similar ranking on how well the user knows that item, e.g. “California, Sacramento, 5, 2” (where the user has seen that question 5 times, and got it right 2 times).

Could someone perhaps point me to a sample of what such a file would look like?

Question-Blob 2 is:
Once I have tabs called things like “State Capitals” and “U.S. Presidents”, how do I access one of these from my main program?

I’m assuming it’s best to start by reading one tab’s data into a table, and then working from that. Is that correct?
(And if I do it this way, could I later write back out to the data, such as to update the item scores?)

Is iterating through the data the best way to store it into a table, or is there a way to grab the whole list at once?

Is “table” even the right word for what I’d be creating? Is that how I’d store a list of x items, each item having y pieces of data associated with it?

To assign each item, I think I could write something like:

quiz = {}
quiz[“California”] = “Sacramento”

But how do I create a table where “California” is tied to several values that I can update independently (so that I can track the user’s score and progress)?

I’ve seen various combinations of square brackets, curly brackets, periods, and colons, and my head is swimming. I’ve looked in some of the wonderful sample apps posted here, but the ones I’ve found are complicated (though delightful) and I’m afraid I need something more basic.

I’d be ever so grateful if someone could point me to some ultra-basic examples or documentation, or earn bonus chocolate cookies by typing up a couple lines of ever-so-elegant code that points me in the right direction. Thanks much for any and all help!

Thanks Mark!

Fiddling around, here’s one approach you could take. It makes the assumption that your quiz consists of key, value pairs and the whole quiz has an overall prompt good for all questions.

Quiz = class()

function Quiz:init(p)
    self.prompt = p
    self.items = {}
    self.currentItem = 0
end

function Quiz:addItem(q, a)
    local item
    item = QuizItem(q, a)
    table.insert(self.items, item)
end

function Quiz:drawQuestion(x, y)
    self.items[self.currentItem]:drawQuestion(self.prompt, x, y)
end

function Quiz:drawAnswer(x, y)
    self.items[self.currentItem]:drawAnswer(x, y)
end

QuizItem = class()

function QuizItem:init(q, a)
    -- you can accept and set parameters here
    self.question = q
    self.answer = a
end

function QuizItem:drawQuestion(prompt, x, y)
    text(prompt..self.question.."?", x, y)
end

function QuizItem:drawAnswer(x, y)
    text(self.answer, x, y)
end
--Main

function setup()
    states = Quiz("What is the capitol of ")
    states:addItem("Kentucky", "Frankfort")
    states:addItem("Missouri", "Jefferson City")
    
    presidents = Quiz("Who was the ")
    presidents:addItem("1st president", "George Washington")
    presidents:addItem("16th president", "Abraham Lincoln")
    
    states.currentItem = 1
    presidents.currentItem = 1
end

function draw()
    -- This sets a dark background color 
    background(40, 40, 50)
    textMode(CORNER)
    textAlign(LEFT)
    fill(255, 255, 255, 255)
    states:drawQuestion(10, HEIGHT - 50)
    states:drawAnswer(50, HEIGHT - 80)
    presidents:drawQuestion(10, HEIGHT - 150)
    presidents:drawAnswer(50, HEIGHT - 180)
    strokeWidth(5)

    -- Do your drawing here
    
end

Just saw the new version of Codea posted, and would like to resume my attempts at learning. Is anyone up for suggesting how a data file should be structured, if it’s to contain data pairs and some other information (e.g. “California”, “Sacramento”, “quizzed 3 times”, “got correct 2 times”)? I get that I could hard-code the data into the app, but I’d like a more flexible approach that would make it easy for me (or other users) to paste in new data sets. Thanks for any and all advice!