Solarus-Games English Forum

Solarus => Development => Topic started by: zutokaza on September 25, 2016, 09:40:08 AM

Title: Is it possible to grab certain place values in a variable?
Post by: zutokaza on September 25, 2016, 09:40:08 AM
Is it possible to grab certain place values in a variable?

Example:

soda = 9478

punch = soda place[4]

print(punch)

9

Another example:

punch = soda place[3]

print(punch)

4

I would like to grab 9,4,7, and 8 place values from soda and assign them to variables.
Title: Re: Is it possible to grab certain place values in a variable?
Post by: Zefk on September 25, 2016, 10:04:47 AM
This link might help.
http://www.cplusplus.com/forum/beginner/50059/

2534

2534/1000 = 2

place_4/1000

2534%1000 = 534

place_4%1000 = place_3_full

534/100 = 5

place_3_full/100 = place_3

534%100 = 34

place_3_full%100 = Place_2_full

34/10 = 3

place_2_full/10 = Place_2

34%10 = 4

place_2_full%10 = place_1_full

4/1 = 4

place_1_full/1 = place_1

----------------------------------
You will have to tweak it for correct syntax and order depending on your process.
Title: Re: Is it possible to grab certain place values in a variable?
Post by: Diarandor on September 25, 2016, 10:56:52 AM
You should check some Lua tutorial, since there are many Lua functions for strings:
http://lua-users.org/wiki/StringLibraryTutorial

The easiest way is getting a substring with:
Code (Lua) Select
string.sub(s, i [, j])
or equivalently,
Code (Lua) Select
s:sub(i [,j])
where "s" is the variable where you have your string, "i" the initial position and "j" the (optional) end position.
It's just one line of code. Note that Lua automatically converts strings to integers when it is necessary, so you will not need to convert things.
Title: Re: Is it possible to grab certain place values in a variable?
Post by: llamazing on September 25, 2016, 11:25:50 AM
The easiest way to do it would be to convert soda to a string and then get each place value as a substring like Diarandor suggests.

For example:
Code (lua) Select

local soda = 9478
local soda_string = string.format("%04d", soda) --soda_string = "9478"

local soda_split = {}
for i = 0,3 do
local index = soda_string:len()-i
local num = tonumber(soda_string:sub(index,index))
table.insert(soda_split, num)
end

--soda_split[1] = 8
--soda_split[2] = 7
--soda_split[3] = 4
--soda_split[4] = 9


The "%04d" adds leading zeroes to the number converted to a string, so 1 becomes "0001" and 12345 becomes"12345". That ensures you'll get a value of zero rather than nil if soda is less than 4 digits.
Title: Re: Is it possible to grab certain place values in a variable?
Post by: Diarandor on September 25, 2016, 11:53:32 AM
Well, there are actually maaaany ways to put the digits of a number in a string. I have not tested them, but other ways could be:
Code (Lua) Select

local soda = 9478
local soda_split = {}

for digit in string.gmatch( tostring(soda), "%d" ) do
  table.insert(soda_split, digit)
end


and

Code (Lua) Select

local soda = 9478
local soda_split = {}

local num = soda
if num == 0 then soda_split = {0} end
while num > 0 do
  local digit = num % 10
  num = math.floor(num / 10)
  table.insert(soda_split, digit)
end
Title: Re: Is it possible to grab certain place values in a variable?
Post by: zutokaza on September 25, 2016, 07:56:12 PM
All posts solve the problem. Thank you everyone!  :D