Hello everyone,
I have a Lua-related question, maybe someone out there will be able to help me with.
I have a skin which reads some information from a .inc file, then sort them in alphabetical order, using a Lua script. Finally it should write these sorted information into another .inc file (obviously in sorted order). The problem is that the code reads the information, the script sorts them, but when they are written into the new file, their order is random. I know the information have been sorted, because I added a simple String meter, to see the information and they are indeed sorted, however when written into the file, as said, their order has nothing to do with the order of the sorted information.
I used the ReadFileLines and the WriteIni functions from the Rainmeter Snippets, to read and write the files. I attach a simplified skin, which besides the Sort.ini skin file the contains the MyLua.lua script file and an Events.inc file, which contains the strings (in fact variables) which should be sorted (ten such variables). No need to sort them by the values set for the variables, instead they should be sorted by their name. As you can see in the skin, they are sorted in a proper order, however when written into the d:\Written.inc file (please alter the path in the 77th line of the MyLua.lua script file, if needed - yep, I know there are much more elegant ways to deal with this file, but my question is not related to this detail), their order has nothing to do with the sorted order.
Does anyone know how can I get those variables written into the sorted order?
Many thanks in advance.
It is currently October 14th, 2024, 9:37 pm
Sort information in lua
-
- Rainmeter Sage
- Posts: 16658
- Joined: October 11th, 2010, 6:27 pm
- Location: Gheorgheni, Romania
Sort information in lua
You do not have the required permissions to view the files attached to this post.
-
- Posts: 120
- Joined: November 3rd, 2019, 12:00 am
Re: Sort information in lua
It seems that the ploblem is for key, value in pairs(table) do ... end.
I can use the following code
or there are some solutions (I need more time to understand these pages, but you might learn something from that.)
> Ordered Table Simple | http://lua-users.org/wiki/OrderedTableSimple
> Ordered Table | http://lua-users.org/wiki/OrderedTable
Code: Select all
function WriteIni(inputtable, filename)
assert(type(inputtable) == 'table', ('WriteIni must receive a table. Received %s instead.'):format(type(inputtable)))
local file = assert(io.open(filename, 'w'), 'Unable to open ' .. filename)
local lines = {}
for section, contents in pairs(inputtable) do
table.insert(lines, ('\[%s\]'):format(section))
for key, value in pairs(contents) do
table.insert(lines, ('%s=%s'):format(key, value))
end
table.insert(lines, '')
end
file:write(table.concat(lines, '\n'))
file:close()
end
You can see the behavior of pairs at the https://www.lua.org/cgi-bin/demo using the following code. The result is randomly orderd outputted by each clicking the [run] button.Lua 5.1 manual wrote: The order in which the indices are enumerated is not specified, even for numeric indices. (To traverse a table in numeric order, use a numerical for or the ipairs function.) > pairs (t)
Code: Select all
local t = { a='a1', b='b2', c='c3', d='d4' }
for k,v in pairs(t) do print(k,v) end
Code: Select all
local t = { {a='a1'}, {b='b2'} , {c='c3'}, {d='d4'} }
for i=1, #t do
for k,v in pairs(t[i]) do
print(k,v)
end
end
> Ordered Table Simple | http://lua-users.org/wiki/OrderedTableSimple
> Ordered Table | http://lua-users.org/wiki/OrderedTable
-
- Rainmeter Sage
- Posts: 8465
- Joined: February 27th, 2015, 2:38 pm
- Location: Terra Yincognita
Re: Sort information in lua
Nek is correct, this is a classic thing in Lua: only numerically indexed tables produce the order one expects when building the table. If you need non numeric stuff to be part of the table, place them as values, and not as indexes, i.e. some table[5]={'foo','bar'} instead of sometable['foo']='bar'. You'll have some extra table dimension added in some cases, but your table will have the order you expect, according to the numerical index.
P.S. I'm on my phone ATM so I didn't check the code, so I don't know if there are other problems than the one above. That one though is typically the culprit for what you described.
P.S. I'm on my phone ATM so I didn't check the code, so I don't know if there are other problems than the one above. That one though is typically the culprit for what you described.
-
- Rainmeter Sage
- Posts: 16658
- Joined: October 11th, 2010, 6:27 pm
- Location: Gheorgheni, Romania
Re: Sort information in lua
Maybe I am missing something, but I say this is exactly what I did in the lines 60 - 65 in the MyLua.lua. Or am I wrong?nek wrote: ↑July 14th, 2023, 12:13 pm You can see the behavior of pairs at the https://www.lua.org/cgi-bin/demo using the following code. The result is randomly orderd outputted by each clicking the [run] button.I can use the following codeCode: Select all
local t = { a='a1', b='b2', c='c3', d='d4' } for k,v in pairs(t) do print(k,v) end
Code: Select all
local t = { {a='a1'}, {b='b2'} , {c='c3'}, {d='d4'} } for i=1, #t do for k,v in pairs(t[i]) do print(k,v) end end
-
- Posts: 120
- Joined: November 3rd, 2019, 12:00 am
Re: Sort information in lua
lines 60 - 65 of the MyLua.lua
Code: Select all
for i = 1,#Contents do
if (Contents[i] ~= '[Variables]') then
Options[j], Values[j] = SplitString(Contents[i], '=')[1], SplitString(Contents[i], '=')[2]
j = j + 1
end
end
I meant the code is something like:
Sort.ini
For example, read a file and create a table in function Initialize() -> function ReadFile() and display using Inline lua.
-
- Rainmeter Sage
- Posts: 16658
- Joined: October 11th, 2010, 6:27 pm
- Location: Gheorgheni, Romania
Re: Sort information in lua
This is great! Definitely seems to be working as expected. Have to study your solution a little bit, but at a first look, seems to do what I intended, in the way I intended.
Thank you very much for this help. Means a lot.
-
- Rainmeter Sage
- Posts: 8465
- Joined: February 27th, 2015, 2:38 pm
- Location: Terra Yincognita
Re: Sort information in lua
It's important to remember that this is not just a one time particularity of Lua code for this case, you'll need it in other situations too. Whenever the order in the table matters, use numerical indexing and create the table structure in a way that matches that. If order doesn't matter, the other way involving plain pairs() - as opposed to ipairs() and numerical indexing - is very much valid and sometimes even faster.
-
- Rainmeter Sage
- Posts: 16658
- Joined: October 11th, 2010, 6:27 pm
- Location: Gheorgheni, Romania
Re: Sort information in lua
Alright, thanks both of you for all the provided information. Makes sense.Yincognito wrote: ↑July 14th, 2023, 8:51 pm It's important to remember that this is not just a one time particularity of Lua code for this case, you'll need it in other situations too. Whenever the order in the table matters, use numerical indexing and create the table structure in a way that matches that. If order doesn't matter, the other way involving plain pairs() - as opposed to ipairs() and numerical indexing - is very much valid and sometimes even faster.
-
- Rainmeter Sage
- Posts: 8465
- Joined: February 27th, 2015, 2:38 pm
- Location: Terra Yincognita
Re: Sort information in lua
Nek deserves the thanks on this one entirely. I only mentioned some "principles" when working with these things, really.
-
- Rainmeter Sage
- Posts: 16658
- Joined: October 11th, 2010, 6:27 pm
- Location: Gheorgheni, Romania
Re: Sort information in lua
I thank for any kind of information, to both of you.Yincognito wrote: ↑July 14th, 2023, 9:24 pm Nek deserves the thanks on this one entirely. I only mentioned some "principles" when working with these things, really.