Author Topic: Is it possible to grab certain place values in a variable?  (Read 1682 times)

zutokaza

  • Full Member
  • ***
  • Posts: 146
  • Active - Making stories can take the most time.
    • View Profile
Is it possible to grab certain place values in a variable?
« 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.

Zefk

  • Hero Member
  • *****
  • Posts: 536
  • Just helping Solarus
    • View Profile
    • Zelzec
Re: Is it possible to grab certain place values in a variable?
« Reply #1 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.

Diarandor

  • Hero Member
  • *****
  • Posts: 1047
  • Cats are cool! (ΦωΦ)
    • View Profile
Re: Is it possible to grab certain place values in a variable?
« Reply #2 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.
“If you make people think they're thinking, they'll love you. But if you really make them think, they'll hate you.”

llamazing

  • Full Member
  • ***
  • Posts: 198
    • View Profile
Re: Is it possible to grab certain place values in a variable?
« Reply #3 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.

Diarandor

  • Hero Member
  • *****
  • Posts: 1047
  • Cats are cool! (ΦωΦ)
    • View Profile
Re: Is it possible to grab certain place values in a variable?
« Reply #4 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
“If you make people think they're thinking, they'll love you. But if you really make them think, they'll hate you.”

zutokaza

  • Full Member
  • ***
  • Posts: 146
  • Active - Making stories can take the most time.
    • View Profile
Re: Is it possible to grab certain place values in a variable?
« Reply #5 on: September 25, 2016, 07:56:12 pm »
All posts solve the problem. Thank you everyone!  :D