-1

So I'm trying to make a table with numbers in them, but also make the exact same thing but only that its a string with the numbers so doing print(table[i]) doesn't solve the problem.

local problems = {
  1 + 1,
  2 + 2,
}

local stringProblems = {
  "1+ 1",
  "2 + 2",
}

How would I go about doing this?

KLB
  • 1
  • 2
  • Start with `stringProblems` and populate `problems` by evaluating each expression using `load("return "..s)()`. – lhf Sep 15 '21 at 21:59
  • I mean like how would I make the problems a string without having to make a stringProblems table? – KLB Sep 15 '21 at 22:37

1 Answers1

4
local problems = {
  1 + 1,
  2 + 2,
}

actually evaluates to local problems = {2, 4}

You cannot make

local stringProblems = {
  "1+ 1",
  "2 + 2",
}

out of it. The arithmetic expressions are evaluated when you define the table. Any information how you got those values is lost. So Lua does not know if stringProblems is

{
  "1+ 1",
  "2 + 2",
}

or

{
  "399 / 399",
  "1 + 3"
}

or whatever. You can only do it the other way as lhf suggested. Have the strings first.

Piglet
  • 27,501
  • 3
  • 20
  • 43