Jump to content
  • Advertisement
Sign in to follow this  
PGSCreativeDirector

Unity String:gmatch in lua problem

This topic is 2475 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

[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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!