Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LTable length function doesn't work correctly in some edge cases. #304

Open
mttsner opened this issue Nov 10, 2020 · 2 comments · May be fixed by #329
Open

LTable length function doesn't work correctly in some edge cases. #304

mttsner opened this issue Nov 10, 2020 · 2 comments · May be fixed by #329

Comments

@mttsner
Copy link

mttsner commented Nov 10, 2020

Issue

When using the # unary operator on a lua array that has index = value, the returned length is the highest index.

Example

print(#{
	[20] = 0;
	[600] = 0;
}) --Output: 600

Expected behaviour

print(#{
	[20] = 0;
	[600] = 0;
}) --Output: 0

How the length operator should work

The length operator returns the last numeric key whose value is not nil for tables starting from 1. The length operator returns zero for tables when the first numeric key is nil or there are no numeric keys.

@tongson
Copy link
Contributor

tongson commented Jun 6, 2021

I think this is because OP_LEN iterates from the end like MaxN.

@Egor-Skriptunoff
Copy link

Egor-Skriptunoff commented Jun 24, 2022

How the length operator should work

The length operator returns the last numeric key whose value is not nil for tables starting from 1. The length operator returns zero for tables when the first numeric key is nil or there are no numeric keys.

No.
According to Lua manual, the length operator applied on a table returns a border in that table. A border in a table is any positive integer index present in the table that is followed by an absent index.

The table {[20]=0, [600]=0} has 3 borders: 0, 20 and 600.
So, #{[20]=0, [600]=0} may return any of these 3 numbers, all of them are correct.

See table length definition

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants