String:gmatch in lua problem

Started by
3 comments, last by PGSCreativeDirector 12 years, 5 months ago
[font="Lucida Console"]could someone tell me how to fix this i don't see the problem ?[/font]

[font="Lucida Console"]This is my problem:[/font]

[font="Lucida Console"]I can't after every gmatch occurrence write to a textfile for every occurence of .txt [/font]


[font="Lucida Console"]What are you using?[/font]
[font="Lucida Console"]i'm using gmatch to find .txt, which .txt is in a textfile eg ( " what i mean is a textfile of it contents"). [/font]

[font="Lucida Console"]Then writing to another textfile for every occurrence of .txt [/font][font="Lucida Console"] this would output like this eg( textfile1,textfile2,textfile3 and so on). [/font]


Heres My Code


require "lfs"


file = io.open("ListingDirectoryList.txt","w")

input = io.open("AmountofTextFiles.txt","w")

for content in lfs.dir ("content") do file:write(content, " ")
end



file:close()




for line in io.lines("ListingDirectoryList.txt") do if line:gmatch(".txt","%a+") then do input:write("textfile", +1)
end
end
end






input:close()


Could anyone tell me how to fix this


I took a look at the lua reference and lua programming and couldn't find any more information then the functions explanations themselves and it just really annoying for me so im asking the community
Advertisement
I'm really not sure if I understand what you're asking, but you probably want something like this:



-- Assuming input file is just a list of file names + extensions, with only one per line.

local input = io.open("test.txt", "w")

local output = ""

for line in input:lines() do
local textfile = line:match("(%w+)%.txt")

if textfile then
output = output .. textfile .. "\n"
end
end

input:close()

print(output)



I leave outputting things in the exact way you want for you to do.

Note that gmatch is for iterating over a string, but you probably want to use match instead if you just have one text file per line in the input. Note that there are two ways to call the string.gmatch function: string.gmatch(input_str, pattern) and input_str:gmatch(pattern). You seem to be mixing these in ways that do not make sense. I would also be surprised if just putting +1 as a parameter to output does what you want.

I'm really not sure if I understand what you're asking, but you probably want something like this:

[source lang="lua"]

-- Assuming input file is just a list of file names + extensions, with only one per line.

local input = io.open("test.txt", w)

local output = ""

for line in input:lines() do
local textfile = line:match("(%w+)%.txt")

if textfile then
output = output .. textfile .. "\n"
end
end

input:close()

print(output)

[/source]

I leave outputting things in the exact way you want for you to do.

Note that gmatch is for iterating over a string, but you probably want to use match instead if you just have one text file per line in the input. Note that there are two ways to call the string.gmatch function: string.gmatch(input_str, pattern) and input_str:gmatch(pattern). You seem to be mixing these in ways that do not make sense. I would also be surprised if just putting +1 as a parameter to output does what you want.





Could you leave my code as is and do the same thing as much as possible so i can understand what you written better your using some terms im not aware of at this moment in time soz
Why would he leave your code as-is when your code is broken?
i ment it change it but keep it as close to that as possible without destorying the hole idea

This topic is closed to new replies.

Advertisement