File syntax is something like:
Code: Select all
#Comment
hello: world
number: 10
Code: Select all
#Comment
hello: world
number: 10
Code: Select all
for word in string.gmatch(cfg, "%a:") do
print(word)
end
That's because the pattern you're describing is actually a letter and a colon.Admicos wrote:...when i do this:it prints ONLY the last word before the colon. Do i need to use another pattern?Code: Select all
for word in string.gmatch(cfg, "%a:") do print(word) end
Code: Select all
string.gsub( cfg, '([^#:\n]-):%s*([^\n]+)', function( name, value )
print( name .. ': ' .. value ..'!!' )
-- Put code here
end )
Code: Select all
cfg = [[hello: world
name: bill
va-lua: try this
#comment
Was: sup
YOu can have: ..Spaces!
Or:NoSpaces
Titles: cannot have #, :, or \n
#comments can have spaces
and: still be ignored!
comments: cannot have colons!
lines can start: with spaces.]]
This still shows only the one letter before colorn. But whatever i solved it with davisdude's solution. Thanks to everybody who tried to help!undef wrote:That's because the pattern you're describing is actually a letter and a colon.Admicos wrote:...when i do this:it prints ONLY the last word before the colon. Do i need to use another pattern?Code: Select all
for word in string.gmatch(cfg, "%a:") do print(word) end
So this should print all the letters before a colon including the colon.
Play around with it some more and if you don't find the solution (only keep on reading if you don't want to find it yourself):
The pattern you're looking for is ": %.*"
This reads: A colon, followed by a space, followed by 0 or more characters
Code: Select all
local myConfTable = {
abc = "some string",
num = 1234,
}
return myConfTable
Code: Select all
cfg = require("myConfig")
print(cfg.abc,cfg.num)
I never thinked that! But i fixed my problem with another solution. Thanks!Azhukar wrote:Take a look at how the framework itself handles a config file. A simple .lua file returning a table is ideal for what you're looking for.
myConfig.luamain.luaCode: Select all
local myConfTable = { abc = "some string", num = 1234, } return myConfTable
Code: Select all
cfg = require("myConfig") print(cfg.abc,cfg.num)
Whoops, I forgot to put the capture.Admicos wrote: This still shows only the one letter before colorn. But whatever i solved it with davisdude's solution. Thanks to everybody who tried to help!
Yeah, %. only capture periods. "." captures everything.undef wrote:And apperently "%." does not work for any character but only special characters... what works for words would be "%a+".
Thanks! I think it's pretty fancy myself, although I'm sure it could be improved.undef wrote:However davisdude's pattern is pretty fancy and way more versitile.
Users browsing this forum: Google [Bot] and 5 guests