Is there a way to make 21 out of 2 and 1 ? (example)

Hello!

t= {21}

a = 2

b = 1

ab = a … b

print(ab) – output 21

print(t[1]) --output 21

print(ab == t[1]) – output false

so my t[1] has a value of 21 while my ab has a value of 2 AND 1.

Is there any way (without typing t = {2 … 1}) to get those two values to be the same ?

Thanks in advance!

Greetings

This seems a bit odd.

You’ve created a table, t, which includes a single element, i.e. t[1] = 21. The reason why you are getting “ab == t[1]” is false is because t[1] is an number and ab is a string.

If you want to combine a and b, like you have above, and turn it back into a number, you should do the following:
 

local a = 2 local b = 1 local ab = a..b -- you can check the type of the variable by using print(type(ab)) -- this should output string -- to turn the string back into a number, simply do ab = tonumber(ab) -- now, if you check the type of the variable, you'll get a number. print(type(ab)) -\> output is "number".

Lua has a few basic types. So far you’re involved four of the types:

  1. String

  2. Number

  3. Boolean (True/False)

  4. Table

There are a few others. When you write out a number without quotes such as:

a = 2

You are assigning the “number” 2 to the variable a.

When you do a concatenation operator (…) you are basically writing:

ab = tostring(2) .. tostring(1)

Now the variable ab holds the “string” value of “21”. It does not hold the numeric value of 21. Lua is pretty good at converting numbers and strings back and forth for you, but you have to be careful because “21” does not equal 21.

If you want ab to hold the number 21, you do:

ab = 2 * 10 + 1

Rob

This seems a bit odd.

You’ve created a table, t, which includes a single element, i.e. t[1] = 21. The reason why you are getting “ab == t[1]” is false is because t[1] is an number and ab is a string.

If you want to combine a and b, like you have above, and turn it back into a number, you should do the following:
 

local a = 2 local b = 1 local ab = a..b -- you can check the type of the variable by using print(type(ab)) -- this should output string -- to turn the string back into a number, simply do ab = tonumber(ab) -- now, if you check the type of the variable, you'll get a number. print(type(ab)) -\> output is "number".

Lua has a few basic types. So far you’re involved four of the types:

  1. String

  2. Number

  3. Boolean (True/False)

  4. Table

There are a few others. When you write out a number without quotes such as:

a = 2

You are assigning the “number” 2 to the variable a.

When you do a concatenation operator (…) you are basically writing:

ab = tostring(2) .. tostring(1)

Now the variable ab holds the “string” value of “21”. It does not hold the numeric value of 21. Lua is pretty good at converting numbers and strings back and forth for you, but you have to be careful because “21” does not equal 21.

If you want ab to hold the number 21, you do:

ab = 2 * 10 + 1

Rob