I have table in Lua as follows:
tab = { y = 1, n = 2}
print(tab)
{
y : 1
n : 2
}
I am trying to index it with a string, which I have read from a CSV file. The following works as intended :
print(tab['y'])
1
However, this is not working as expected:
local file = io.open(label_file, "r")
for line in file:lines() do
local col = string.split(line, ",")
print(type(col[2])) -> string
print(col[2]) -> y
print( tab[ (col[2]) ]) -> nil
end
I have tried coercing col[2] to a string, but will still not index my table as intended.
Sorry for the confusion, I wrote a string.split function but neglected to include it above in the code sample.
I have now resolved the error. Earlier, I had written the CSV file using Matlab and the cells were formatted incorrectly as 'numbers'. Upon changing the formatting to 'text', the code works as intended. A very odd error in my opinion, that leads to this kind of thing:
print(type(col[2])) -> string
print(col[2]) -> y
print( col[2] == 'y') -> false
print(string.byte('y'))andprint(string.byte(col[2], 1, #col[2]))- moteus