Add @ to be used as length of sparse table

Things that already exist in the current mod API
Post Reply
Honktown
Smart Inserter
Smart Inserter
Posts: 1025
Joined: Thu Oct 03, 2019 7:10 am
Contact:

Add @ to be used as length of sparse table

Post 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 )
I have mods! I guess!
Link

User avatar
Optera
Smart Inserter
Smart Inserter
Posts: 2916
Joined: Sat Jun 11, 2016 6:41 am
Contact:

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

Post 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?

User avatar
Klonan
Factorio Staff
Factorio Staff
Posts: 5150
Joined: Sun Jan 11, 2015 2:09 pm
Contact:

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

Post by Klonan »

We already have `table_size` function, which does just that

Honktown
Smart Inserter
Smart Inserter
Posts: 1025
Joined: Thu Oct 03, 2019 7:10 am
Contact:

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

Post 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
I have mods! I guess!
Link

User avatar
Optera
Smart Inserter
Smart Inserter
Posts: 2916
Joined: Sat Jun 11, 2016 6:41 am
Contact:

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

Post 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

Honktown
Smart Inserter
Smart Inserter
Posts: 1025
Joined: Thu Oct 03, 2019 7:10 am
Contact:

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

Post 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.
I have mods! I guess!
Link

Bilka
Factorio Staff
Factorio Staff
Posts: 3128
Joined: Sat Aug 13, 2016 9:20 am
Contact:

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

Post 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.
I'm an admin over at https://wiki.factorio.com. Feel free to contact me if there's anything wrong (or right) with it.

User avatar
darkfrei
Smart Inserter
Smart Inserter
Posts: 2903
Joined: Thu Nov 20, 2014 11:11 pm
Contact:

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

Post by darkfrei »

Can this function be called as table.size()?

Rseding91
Factorio Staff
Factorio Staff
Posts: 13202
Joined: Wed Jun 11, 2014 5:23 am
Contact:

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

Post by Rseding91 »

darkfrei wrote:
Wed Feb 19, 2020 8:13 am
Can this function be called as table.size()?
No.
If you want to get ahold of me I'm almost always on Discord.

Post Reply

Return to “Already exists”