It is currently March 29th, 2024, 8:48 am

Lua read file create variables

Discuss the use of Lua in Script measures.
syngod
Posts: 13
Joined: May 3rd, 2012, 2:10 pm

Lua read file create variables

Post by syngod »

Hey guys i have another topic here http://rainmeter.net/forum/viewtopic.php?f=5&t=12049&p=68816#p68816

But since this is an lua specific questions i thought it best to ask here.

I am having trouble with reading the file that i want to create. I thought to read the file the create a variable that i can use that follows a typesale.target from this file.

Code: Select all

<machinesales>
   <target>100000</target>
   <actual>6500</actual>
   <projected>250000</projected>
</machinesales>

<aftermarketsales>
   <target>50000</target>
   <actual>10000</actual>
   <projected>60000</projected>
</aftermarketsales>

<repairsales>
   <target>50000</target>
   <actual>10000</actual>
   <projected>60000</projected>
</repairsales>
So for each of those they would have a machinesales.target and so on.

Here is what i have in lua so far.

Code: Select all

function Initialize()

   sFileName = SELF:GetOption('FileName')
   
end -->Initialize

function Update()

   
   hFile = io.open(sFileName)
   if not hFile then print('File error'); return 'File error'; end 
   io.close(hFile)   
   
   sale = {}   

  for lines in io.lines(FileName)
      table.insert(lines, sale)
	  Need something here to create typesale.target typesale.actual
    end

   return (#sale)

end -->Update
But the more i try this way the more i find myself in a corner. Is there a better way to do this.

What i want to do with the end result is use the numbers in a bargraph meter that will show progress of the sales area. i hope this makes sense.
User avatar
smurfier
Moderator
Posts: 1931
Joined: January 29th, 2010, 1:43 am
Location: Willmar, MN

Re: Lua read file create variables

Post by smurfier »

Is the file that particular set format or can it be changed a little?

This would be much easier to parse:

Code: Select all

<machinesales target="100000" actual="6500" projected="250000"/>

<aftermarketsales target="50000" actual="10000" projected="60000"/>

<repairsales target="50000" actual="10000" projected="60000"/>
If you can change it like that, then this should do the trick.

Code: Select all

function Initialize()
	sFileName = SELF:GetOption('FileName')
end -- Initialize

function Update()
	hFile = io.open(sFileName)
	if not io.type(hFile)=='file' then
		print('File error')
		return 'File error'
	else
		sale = {}
		local text=io.read('*all')
		io.close(hFile)
		for tag,contents in string.gmatch(text,'<([^%s>]+)(.-)%/>') do
			sale[string.lower(tag)]=Keys(contents,{target=0,actual=0,projected=0})
		end
	end
	return #sale

end -- Update

function Keys(a,b) -- Converts Key="Value" sets to a table
	local tbl=b or {}
	string.gsub(a,'(%a+)=(%b"")',function(c,d)
		local strip=string.match(d,'"(.+)"')
		tbl[string.lower(c)]=tonumber(strip) or strip
	end)
	return tbl
end -- Keys
GitHub | DeviantArt | Tumblr
This is the song that never ends. It just goes on and on my friends. Some people started singing it not knowing what it was, and they'll continue singing it forever just because . . .
syngod
Posts: 13
Joined: May 3rd, 2012, 2:10 pm

Re: Lua read file create variables

Post by syngod »

smurfier wrote:Is the file that particular set format or can it be changed a little?

This would be much easier to parse:

Code: Select all

<machinesales target="100000" actual="6500" projected="250000"/>

<aftermarketsales target="50000" actual="10000" projected="60000"/>

<repairsales target="50000" actual="10000" projected="60000"/>
If you can change it like that, then this should do the trick.

Code: Select all

function Initialize()
	sFileName = SELF:GetOption('FileName')
end -- Initialize

function Update()
	hFile = io.open(sFileName)
	if not io.type(hFile)=='file' then
		print('File error')
		return 'File error'
	else
		sale = {}
		local text=io.read('*all')
		io.close(hFile)
		for tag,contents in string.gmatch(text,'<([^%s>]+)(.-)%/>') do
			sale[string.lower(tag)]=Keys(contents,{target=0,actual=0,projected=0})
		end
	end
	return #sale

end -- Update

function Keys(a,b) -- Converts Key="Value" sets to a table
	local tbl=b or {}
	string.gsub(a,'(%a+)=(%b"")',function(c,d)
		local strip=string.match(d,'"(.+)"')
		tbl[string.lower(c)]=tonumber(strip) or strip
	end)
	return tbl
end -- Keys

No i think that will work for me perfectly... Do you no that will return the numbers in a variable form correct so machinessales.target=100000 right
User avatar
smurfier
Moderator
Posts: 1931
Joined: January 29th, 2010, 1:43 am
Location: Willmar, MN

Re: Lua read file create variables

Post by smurfier »

It should be sales.machinesales.target

Basically this is what the table looks like, though this is not the method we can use to create it.

Code: Select all

sales={
	machinesales={
		target=100000,
		actual=6500,
		projected=250000,
	}
	aftermarketsales={
		target=50000,
		actual=10000,
		projected=60000,
	}
	repairsales={
		target=50000,
		actual=10000,
		projected=60000,
	}
}
GitHub | DeviantArt | Tumblr
This is the song that never ends. It just goes on and on my friends. Some people started singing it not knowing what it was, and they'll continue singing it forever just because . . .