1

I'm trying to create a list of integers, similar to python where one would say

x = input("Enter String").split() # 1 2 3 5
x = list(map(int,x)) # Converts x = "1","2",3","5" to x = 1,2,3,5

Here's my code asking for the input, then splitting the input into a table, i need help converting the contents of the table to integers as they're being referenced later in a function, and i'm getting a string vs integer comparison error. I've tried changing the split for-loop to take a number but that doesn't work, I'm familiar with a python conversion but not with Lua so I'm looking for some guidance in converting my table or handling this better.

 function main()
  print("Hello Welcome the to Change Maker - LUA Edition")
  print("Enter a series of change denominations, separated by spaces")
  input = io.read()
  deno = {}
  for word in input:gmatch("%w+") do table.insert(deno,word) end
end
--Would This Work?:
--for num in input:gmatch("%d+") do table.insert(deno,num) end
Rob
  • 403
  • 9
  • 19
  • Possible duplicate of [Lua string to int](https://stackoverflow.com/questions/10962085/lua-string-to-int) – Piglet Mar 12 '18 at 17:04

1 Answers1

4

Just convert your number-strings to numbers using tonumber

local number = tonumber("1")

So

 for num in input:gmatch("%d+") do table.insert(deno,tonumber(num)) end

Should do the trick

Piglet
  • 27,501
  • 3
  • 20
  • 43