I have this code which reads a txt file which contains simply a list of word pairs on each line separate by a space and for every line, the code will split the word pair into separate word and print each word one after the other:
file = io.open( "test.txt", "r" )
temp = {}
for line in file:lines() do
for word in line:gmatch("%w+") do
print(word)
end
end
file:close()
sample test.txt
big small
tall short
up down
left right
output
big
small
tall
short
up
down
left
right
However, I find myself needing to split each word in each word pair into separate variables such that I can use an if statement on word1 in order to do something with word2.
Something like:
file = io.open( "test.txt", "r" )
temp = {}
for line in file:lines() do
for word in line:gmatch("%w+") do
word1 = first word
word2 = second word
if (word1 = "tall") then
print(word2)
end
end
end
file:close()