The best way to print just numbers is to use either tonumber() or type().
Example:
(Using tonumber())
local _table = {
1,
2,
"gorilla",
3,
4,
}
for i,v in pairs(_table) do
if tonumber(v) then
print(v)
end
end
What the above does is this:
tonumber is a function that converts strings to numbers; if it can .
By using an if statement, I am testing whether or not the conversion went through. If it did, the statement will be true, and it will print the number; if not, the value will not print.
Note: if you call this function while the value is a number (like, try to convert a number to a number), it will still return true; and the number will print.
(Using type())
local _table = {
1,
2,
"gorilla",
3,
4,
}
for i,v in pairs(_table) do
if type(v) == "number" then
print(v)
end
end
What the above does is this:
type is a function that returns the type of value the argument is. If the argument is "gorilla", then it will return string; however, if the argument is 1, then it will return number.
type cannot determine, though, the difference between 1 and "1" (the number one and a stringified one) considering the stringified one is a string.
I am using the if statement to ensure that the returned type, is a number. If it is, the statement is true, and the number is printed, if not, then the for loop "skips" (iterates) again.
Resources:
tonumber()
type()
1 comment
Is that the real script? or is this script just used as an example? Posting your real script might be better with this question as different Value types can be sifted with numerous ways