Mmm sorry but wouldn't [manual]table.sort[/manual] work?HugoBDesigner wrote:I wrote a simple array organizer for numerical entries. I know these things come with Lua already, but after I wrote one for strings (long ago, dunno where to find it), I figured I'd give numbers a try:
Mini Functions Repository
- Positive07
- Party member
- Posts: 1014
- Joined: Sun Aug 12, 2012 4:34 pm
- Location: Argentina
Re: Mini Functions Repository
for i, person in ipairs(everybody) do
[tab]if not person.obey then person:setObey(true) end
end
love.system.openURL(github.com/pablomayobre)
[tab]if not person.obey then person:setObey(true) end
end
love.system.openURL(github.com/pablomayobre)
- HugoBDesigner
- Party member
- Posts: 403
- Joined: Mon Feb 24, 2014 6:54 pm
- Location: Above the Pocket Dimension
- Contact:
Re: Mini Functions Repository
Yeah, it would, but it was mostly experimental. I already knew of table.sort, but figured I'd give it a try to see how it'd work.
Speaking of that, I finally found the alphabetical organizer: it was in my Minigame Simulator all along
And to use it:
Speaking of that, I finally found the alphabetical organizer: it was in my Minigame Simulator all along
Code: Select all
function alphabetic_order(a, b)
local g = "abcdefghijklmnopqrstuvwxyz"
local alphabet = {}
for i = 1, string.len(g) do table.insert(alphabet, string.sub(g, i, i)) end
local a, b = string.lower(a), string.lower(b)
for j = 1, math.min(string.len(a), string.len(b)) do
local aa, bb = string.sub(a, j, j), string.sub(b, j, j)
if aa ~= bb then
if table.contains(alphabet, aa) and table.contains(alphabet, bb) then
if table.contains(alphabet, aa) < table.contains(alphabet, bb) then
return true
elseif table.contains(alphabet, aa) > table.contains(alphabet, bb) then
return false
end
elseif table.contains(alphabet, aa) then
return false
else
return true
end
elseif j == math.min(string.len(a), string.len(b)) then --virtually similar, return the smallest as first
return (string.len(a) < string.len(b) and true) or false
end
end
end
Code: Select all
table.sort(table, function(a, b)
return alphabetic_order(table[a], table[b])
end)
- Positive07
- Party member
- Posts: 1014
- Joined: Sun Aug 12, 2012 4:34 pm
- Location: Argentina
Re: Mini Functions Repository
You can take this part out of the function since it is constant:
Then again, you can compare two strings in Lua with > and < and they sort alphabetically, not sure about caps but you can always do
Code: Select all
local g = "abcdefghijklmnopqrstuvwxyz"
local alphabet = {}
for i = 1, string.len(g) do table.insert(alphabet, string.sub(g, i, i)) end
Code: Select all
table.sort(t, function (a, b)
return string.lower(a) < string.lower(b)
end)
for i, person in ipairs(everybody) do
[tab]if not person.obey then person:setObey(true) end
end
love.system.openURL(github.com/pablomayobre)
[tab]if not person.obey then person:setObey(true) end
end
love.system.openURL(github.com/pablomayobre)
Re: Mini Functions Repository
How 'bout this?
Use it in the same way:
Plus, it doesn't rely on table.contains
*edit: Thanks to Positive07 for teaching me you can compare strings with '<' and '>'!
Code: Select all
function alphabetic_sort(a,b)
-- Handle words that are the same until a certain point
local i = 1
while a:sub( i, i ) == b:sub( i, i ) do
i = i + 1
-- No letter goes before words with letters
if i > #a then return true end
if i > #b then return false end
end
return a:sub( i, i ) < b:sub( i, i )
end
Code: Select all
table.sort( tab, alphabetic_sort )
-- Reverse alphabetized
table.sort( tab, function( a, b ) return not alphabetic_sort( a, b ) end )
*edit: Thanks to Positive07 for teaching me you can compare strings with '<' and '>'!
GitHub | MLib - Math and shape intersections library | Walt - Animation library | Brady - Camera library with parallax scrolling | Vim-love-docs - Help files and syntax coloring for Vim
- Positive07
- Party member
- Posts: 1014
- Joined: Sun Aug 12, 2012 4:34 pm
- Location: Argentina
Re: Mini Functions Repository
I'm pretty sure you can do that just with table.sort:
Note, uppercase are sorted first and then lower case, that is "S" is lower than "s" so if you need to ignore case then use the code I posted before:
Code: Select all
local t = {
"something",
"somewhere",
"someone",
"someday",
"some",
"so",
"s",
"abc"
}
table.sort(t)
for i, v in ipairs(t) do
print(i, v)
end
--Output:
--1 abc
--2 s
--3 so
--4 some
--5 someday
--6 someone
--7 something
--8 somewhere
Code: Select all
table.sort(t, function (a,b) return string.lower(a) < string.lower(b) end)
for i, person in ipairs(everybody) do
[tab]if not person.obey then person:setObey(true) end
end
love.system.openURL(github.com/pablomayobre)
[tab]if not person.obey then person:setObey(true) end
end
love.system.openURL(github.com/pablomayobre)
- zorg
- Party member
- Posts: 3468
- Joined: Thu Dec 13, 2012 2:55 pm
- Location: Absurdistan, Hungary
- Contact:
Re: Mini Functions Repository
String sorting probably follows the underlying encoding's numerical values, and since the first 127 should be somewhat standard across all "ansi" codepages (or, if you want me to be utterly explicit, will be equivalent to ISO/IEC_8859-1 and/or Windows-1252), the order of 0-9A-Za-z will be adhered to. Don't quote me on this though.
Me and my stuff True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
- Positive07
- Party member
- Posts: 1014
- Joined: Sun Aug 12, 2012 4:34 pm
- Location: Argentina
Re: Mini Functions Repository
I quote you on that, that is how it works, and it does it on a character by character basics, so if the first is equal it goes with the next one and so on...
for i, person in ipairs(everybody) do
[tab]if not person.obey then person:setObey(true) end
end
love.system.openURL(github.com/pablomayobre)
[tab]if not person.obey then person:setObey(true) end
end
love.system.openURL(github.com/pablomayobre)
Re: Mini Functions Repository
Here's a pair of functions that allow storing tuples into tables and then retrieving back these tables as tuples:
Example usage:
Code: Select all
local function pack_n(...)
return {select('#', ...), ...}
end
local unpack_n
do
local function unpack_count(x, n)
if n == x[1] + 1 then
return
end
n = n + 1
return x[n], unpack_count(x, n)
end
function unpack_n(a)
return unpack_count(a, 1)
end
end
Code: Select all
local function my_crazy_func()
return nil, 2, 3, nil, "yes", math.acos(3), false, nil
end
-- Store the returned tuple in a table
local packed = pack_n(my_crazy_func())
-- Retrieve the table as a tuple and print it
print(unpack_n(packed))
-- output: nil 2 3 nil yes nan false nil
-- By comparison, using the default Lua unpack fails with the nils:
packed = {my_crazy_func()}
print(unpack(packed))
-- output: nil 2 3
Re: Mini Functions Repository
Lua 5.2 adds a similar "pack" function to the standard table module that's implemented like this:
Code: Select all
local function pack(...)
return {n=select("#", ...), ...}
end
local function getSomeValues()
return 8, nil, "foo"
end
local packed = pack(getSomeValues())
print(unpack(packed, 1, packed.n)) -- 8 nil foo
Tools: Hot Particles, LuaPreprocess, InputField, (more) Games: Momento Temporis
"If each mistake being made is a new one, then progress is being made."
"If each mistake being made is a new one, then progress is being made."
Who is online
Users browsing this forum: Google [Bot] and 2 guests