Unexpected output from getAreaRooms()

Post Reply
visionok
Posts: 22
Joined: Thu Mar 04, 2021 8:50 am

Unexpected output from getAreaRooms()

Post by visionok »

Here's what I get
Code: [show] | [select all] lua
lua getAreaRooms(7)
{
  67,
  64,
  65,
  63,
  [0] = 66
}
I was expecting it to be
Code: [show] | [select all] lua
{
  67,
  64,
  65,
  63,
  66
}
I've tried a number of different areas and they all have that

[0] = nn

entry at the end of the table. Is it supposed to behave that way? I'm using the AppImage v4.10.1.

User avatar
Vadi
Posts: 5035
Joined: Sat Mar 14, 2009 3:13 pm

Re: Unexpected output from getAreaRooms()

Post by Vadi »

Yeah, unfortunately it starts indexing from 0 which is legal, but not the norm in Lua. That is OK at the end of the day.

visionok
Posts: 22
Joined: Thu Mar 04, 2021 8:50 am

Re: Unexpected output from getAreaRooms()

Post by visionok »

That's a shame. I'm using a tempTimer() to call a function to process that table using table.remove(), so each time the timer is activated it removes and process the first item in the table then if table is not empty creates a new tempTimer() to call the function again. table.remove() does not work on the [0] = nn entry so I had to put in a little bit of extra code to handle it.

User avatar
Vadi
Posts: 5035
Joined: Sat Mar 14, 2009 3:13 pm

Re: Unexpected output from getAreaRooms()

Post by Vadi »

Yeah, agreed. We don't build such functions anymore - it's a mistake from the early versions. That said we have made proper equivalents for some that are this way, for example https://wiki.mudlet.org/w/Manual:Lua_Fu ... ExitStubs1 - so we could do it with getAreaRooms as well.

User avatar
SlySven
Posts: 1019
Joined: Mon Mar 04, 2013 3:40 pm
Location: Deepest Wiltshire, UK
Discord: SlySven#2703

Re: Unexpected output from getAreaRooms()

Post by SlySven »

Technically the output:

Code: Select all

lua getAreaRooms(7)
{
  67,
  64,
  65,
  63,
  [0] = 66
}
is actually:

Code: Select all

lua getAreaRooms(7)
{
  [1] = 67,
  [2] = 64,
  [3] = 65,
  [4] = 63,
  [0] = 66
}
The table printing code assumes that when the indexes are monotonically incrementing from 1 it is a Lua list/indexed table and suppresses the index number from the output, until it can't and then it treats the data as a Lua (associative) array and then displays the keys as well as the values - and for that rogue key value of (the number) 0 it shows it at the end as the last (only) key that does not fit the expected series of item keys for a list...

Post Reply