Table key search problem

Hi guys, I’m trying to make a word hunt game and tried these codes it works perfectly on simulator but on my phone only the last word is being found.

 local path = system.pathForFile( "Data/a.txt" ) local file = io.open( path, "r" ) for line in file:lines() do --line = string.sub( line, 1, #line ) --table.insert(dataword,line) aword[line] = true end io.close( file ) file = nil -- search for word if aword[word] then print( "word found" ) else print( "word not found" ) end

I’ve also attached my a.txt file.

it only have these words

ad

as

asa

art

arts

ab


Please help me locate my problem, Thanks in Advance guys

Here is how I made it work.

local aword = {} local path = system.pathForFile( "a.txt" ) local file = io.open( path, "r" ) for line in file:lines() do --line = string.sub( line, 1, #line ) --table.insert(dataword,line) aword[line] = true end io.close( file ) file = nil word = "art" -- any word from the file -- search for word if aword[word] then print( "word found", word ) else print( "word not found" ) end

Changed lines:

local aword = {}

word = "art" -- any word from the file

print( "word found", word )

Thanks for the reply, have you tried it on android?

here’s what I found.
If I manually make my table it works fine both on simulator and android phone

local aword = { “ad”, “as”, “asa”, “art”, “arts”, “ab”} --this works

***************
but if I make my table from the file only the last word is being searched
local aword = {}
local path = system.pathForFile( “a.txt” )
local file = io.open( path, “r” )
for line in file:lines() do
–line = string.sub( line, 1, #line )
–table.insert(dataword,line)
aword[line] = true
end
io.close( file )
file = nil

***************

Nope. Are you debugging it on Android?

Yes, I’ve tried it on my cellphone and tablet but as I’ve said it can only detect the last word

I meant this -> https://docs.coronalabs.com/guide/basics/debugging/index.html#device-debugging-android

Got it working now, I’ve change from tables to lua modules.
Here is the link from an old post :) https://forums.coronalabs.com/topic/949-load-file-data-into-table/

BTW thanks for helping me

Here is how I made it work.

local aword = {} local path = system.pathForFile( "a.txt" ) local file = io.open( path, "r" ) for line in file:lines() do --line = string.sub( line, 1, #line ) --table.insert(dataword,line) aword[line] = true end io.close( file ) file = nil word = "art" -- any word from the file -- search for word if aword[word] then print( "word found", word ) else print( "word not found" ) end

Changed lines:

local aword = {}

word = "art" -- any word from the file

print( "word found", word )

Thanks for the reply, have you tried it on android?

here’s what I found.
If I manually make my table it works fine both on simulator and android phone

local aword = { “ad”, “as”, “asa”, “art”, “arts”, “ab”} --this works

***************
but if I make my table from the file only the last word is being searched
local aword = {}
local path = system.pathForFile( “a.txt” )
local file = io.open( path, “r” )
for line in file:lines() do
–line = string.sub( line, 1, #line )
–table.insert(dataword,line)
aword[line] = true
end
io.close( file )
file = nil

***************

Nope. Are you debugging it on Android?

Yes, I’ve tried it on my cellphone and tablet but as I’ve said it can only detect the last word

I meant this -> https://docs.coronalabs.com/guide/basics/debugging/index.html#device-debugging-android

Got it working now, I’ve change from tables to lua modules.
Here is the link from an old post :) https://forums.coronalabs.com/topic/949-load-file-data-into-table/

BTW thanks for helping me