✓ Accepted answer
pairs() is a key-value-pairs iterator function, meaning in retrospect it is used to traverse through the attributes of an array. The passphrase you have is a String datatype, meaning this loop will not be suitable in this situation.
Instead, we can use another iterator function that is given by a special method of the String class called gmatch, which we can pass a pattern to define how we interpret this string.
local Passphrase = 123456
for Char in tostring(Passphrase):gmatch("[%w%p]") do
print(tonumber(Char))
end
The pattern [%w%p] matches a character that is alphanumeric or punctuated. Since we're not using the + quantifier in conjunction with the set, we will only get one character at a time resulting in this output:
1
2
3
4
5
6
If you desperately need to have the index point, you can construct an array with the pieces pulled from our :gmatch iterator:
local Passphrase = 123456
local Characters = {} do
for Char in tostring(Passphrase):gmatch("[%w%p]") do
table.insert(Characters, tonumber(Char))
end
end
for i,v in pairs(Characters) do
print(i,v)
end
If this helps, don't forget to accept this answer!
1 comment
Thank you! :D