Page 1 of 1

Add @ to be used as length of sparse table

Posted: Sat Feb 01, 2020 6:21 pm
by Honktown
Ran into the annoyance of # not working on sparse tables, and thought it would be convenient to have a symbol for sparse tables.

There are at least four "common" symbols unused in Lua: !, @, $, ?

! is frequently associated with not, which could get confusing

@, common on keyboards, may mean something specific in some programming languages but does not have a common meaning

$ US-centric, not be present on some keyboards

? might be confused with a conditional, confusing if at the front of a name

@ would be equivalent to:

Code: Select all

local @ = function(tbl)
  local i = 0

  for _ in pairs(tbl) do
    i = i + 1
  end

  return i
end
Can't do that for real because @ is not permitted as part of a name (according to https://www.lua.org/cgi-bin/demo )

Re: Add @ to be used as length of sparse table

Posted: Sat Feb 01, 2020 7:34 pm
by Optera
If you need the length you should use an array instead of a sparse table.
In what circumstance would length of a table even matter?

Re: Add @ to be used as length of sparse table

Posted: Sat Feb 01, 2020 8:10 pm
by Klonan
We already have `table_size` function, which does just that

Re: Add @ to be used as length of sparse table

Posted: Sat Feb 01, 2020 10:40 pm
by Honktown
Optera wrote:
Sat Feb 01, 2020 7:34 pm
If you need the length you should use an array instead of a sparse table.
In what circumstance would length of a table even matter?
When shoving random things into a table, and counting them. You have mods, has there never been a time you wanted to check the length of a sparse table of entities?
Klonan wrote:
Sat Feb 01, 2020 8:10 pm
We already have `table_size` function, which does just that
<3

Re: Add @ to be used as length of sparse table

Posted: Sun Feb 02, 2020 7:57 am
by Optera
Honktown wrote:
Sat Feb 01, 2020 10:40 pm
Optera wrote:
Sat Feb 01, 2020 7:34 pm
If you need the length you should use an array instead of a sparse table.
In what circumstance would length of a table even matter?
When shoving random things into a table, and counting them. You have mods, has there never been a time you wanted to check the length of a sparse table of entities?
No, that's why I was asking.
Currently my only use case would be informative e.g. how many signals LTN generated automatically. A simple n=n+1 during table generation handles that just fine and is faster than iterating through the table a 2nd time just to count entries.

That's why I didn't even know about table_size. You learn something new every time. :D

Re: Add @ to be used as length of sparse table

Posted: Sun Feb 02, 2020 9:15 am
by Honktown
Optera wrote:
Sun Feb 02, 2020 7:57 am
No, that's why I was asking.
Currently my only use case would be informative e.g. how many signals LTN generated automatically. A simple n=n+1 during table generation handles that just fine and is faster than iterating through the table a 2nd time just to count entries.

That's why I didn't even know about table_size. You learn something new every time. :D
In my case, I wanted a quick /command for me (and anyone else) to count how many entities are being healed (the mod stops alien healing per tick, delaying it until a settable time after).

Things which need healing:

1) can't be determined in advance
2) have different conditions they could stop being healed (max health, healing rates)
3) may become invalid (die, get vaporized by someone's mod actions)
4) may be damaged again and thus have to be healed longer
5) would otherwise be completely useless to count or keep sorted
It's about as random as a set one could get. A count let's me quickly validate things being added/removed without extra fluff.

Re: Add @ to be used as length of sparse table

Posted: Wed Feb 19, 2020 7:43 am
by Bilka
As Klonan says, use table_size(). If this function is news to you, I recommend reading https://lua-api.factorio.com/latest/Libraries.html.

Re: Add @ to be used as length of sparse table

Posted: Wed Feb 19, 2020 8:13 am
by darkfrei
Can this function be called as table.size()?

Re: Add @ to be used as length of sparse table

Posted: Wed Feb 19, 2020 2:46 pm
by Rseding91
darkfrei wrote:
Wed Feb 19, 2020 8:13 am
Can this function be called as table.size()?
No.