Wire: array:minIndex/maxIndex() does not work correctly with non-numbers?

Created on 6 Jun 2016  Â·  11Comments  Â·  Source: wiremod/wire

e2function number array:minIndex()
e2function number array:maxIndex()

local R1 = array("1", "2", "3", "4")
print("(R1):  Count=" + R1:count() + ",  MinIndex=" + R1:minIndex() + ",  MaxIndex=" + R1:maxIndex() + ",  Min=" + R1:min() + ",  Max=" + R1:max())

local R2 = array(1 = "First", 2 = "Second", 3 = "Third", 4 = "Fourth")
print("(R2):  Count=" + R2:count() + ",  MinIndex=" + R2:minIndex() + ",  MaxIndex=" + R2:maxIndex() + ",  Min=" + R2:min() + ",  Max=" + R2:max())

Output:

(R1): Count=4, MinIndex=1, MaxIndex=4, Min=1, Max=4
(R2): Count=4, MinIndex=1, MaxIndex=1, Min=0, Max=0

Shouldn't R2:maxIndex() return the same number as R1:maxIndex()?
Both of them are in sequential order, 1-4.

Also, how would you loop through a bit more tedious array, using a for-loop?

local R3 = array(0 = "Null", 1 = "Number One", 5 = "High Five", 101 = "Binary")

I have tried to find a function which would return an array of indexes of an array, but I haven't found it (much like array=table:keys() function, but for array).

Most helpful comment

talk about terribly inefficient... why are you using debug mode while not developing extensions?

also, i thought that was fixed already

All 11 comments

Also, how would you loop through a bit more tedious array, using a for-loop?

local R3 = array(0 = "Null", 1 = "Number One", 5 = "High Five", 101 = "Binary")
I have tried to find a function which would return an array of indexes of an array, but I haven't found it (much like array=table:keys() function, but for array).

Use a foreach loop,

foreach(K, V:type = Array){}

As for the issue at hand, there are a few issues with both functions.

For some reason minIndex doesn't use the tonumber converted value and instead uses the default value returned in the array. Your second array should return 0 for the minIndex because those aren't valid numbers, but because of the mistake I pointed out above, it's comparing them by ascii values. This could cause an error if you had an array with number values and string values, as it'd try to compare them

The reason maxIndex is returning 1 is because of the tonumber behavior we're doing. For the first index the index variable is set to it no matter what, this is because no matter the value it's going to be converted to 0. Because all other values are also going to be converted to 0, none of them are going to greater than the current value which is 0, and so the index isn't going to change. If I had to guess you got lucky with it returning one as pairs isn't implicitly going to go in order.

Edit: Also it seems you're confused by what the functions are meant to do. They're meant to return the index of the lowest, or highest number in the array, not return the highest, or lowest index

@bigdogmat

Use a foreach loop,
foreach(K, V:type = Array){}

Nope. That would be _cheating_ in my situation (as why I said using a for-loop), and also foreach is broken in debug mode atm...

Also it seems you're confused by what the functions are meant to do. They're meant to return the index of the lowest, or highest number in the array, not return the highest, or lowest index

Yeah.. I understand that part now (docs have confused me "Returns the index of the smallest value in the array").

So, in any way, if somebody could add a function (like array=array:indexes()) then it would be possible to do it using a for-loop..

So, in any way, if somebody could add a function (like array=array:indexes()) then it would be possible to do it using a for-loop..

I thought that was fixed, unless the pr hasn't been merged yet. Either way there is a way, you just can't use it because you're in debug mode, though I don't understand why, foreach loops are better in almost all situations.

The correct way to iterate such arrays is using a foreach loop. However if you want to be stubborn and do it in a terribly inefficient way, then go ahead and convert your array into a table and then fetch its indexes.

EDIT: That other bit about minIndex using the wrong value is obviously a mistake in the code (a bug) and needs to be fixed. I can't do it right at this moment though

talk about terribly inefficient... why are you using debug mode while not developing extensions?

also, i thought that was fixed already

@TomyLobo Whoops, my mistake I have forgot to sync Wiremod addon... Yep, okay foreach-loop in debug has been fixed, nice.

@Divran

then go ahead and convert your array into a table and then fetch its indexes

How would we do that? There is no array:toTable(), I have tried using vON encode/decode - didn't work out..

You replace @persist R:array with @persist R:table and you're done

On Tue, Jun 7, 2016 at 12:21 AM, CaptainPRICE [email protected]
wrote:

@TomyLobo https://github.com/TomyLobo Whoops, my mistake I have forgot
to sync Wiremod addon... Yep, okay foreach-loop in debug has been fixed,
nice.

—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
https://github.com/wiremod/wire/issues/1137#issuecomment-224106772, or mute
the thread
https://github.com/notifications/unsubscribe/ABaroBdv9e5yGqUBF0S--v4HswXbeJ3rks5qJJ1ggaJpZM4IvKSx
.

@Divran
That's also a nope, because I have to work via array - not table. As I don't have access to an external E2 (working with datasignals). I hope you see where this is going now, without array=array:indexes() function..

Edit:
If there is no other way than using a foreach-loop, then the following is false (from Expression 2 wiki):

Remember there is not just one way to code something, you can accomplish the same task in several ways.

If there is no other way than using a foreach-loop, then the following is false

Well I mean, arrays in E2 aren't meant to be unsequential by design: considering they use default Lua arrays.

That's also a nope, because I have to work via array - not table. As I don't have access to an external E2 (working with datasignals). I hope you see where this is going now, without array=array:indexes() function..

Oh boo hoo you can't use your bad and inefficient solution so you're forced to use the correct, and best solution instead. This is truly terrible.

It would be possible to add array:indexes() but I see no reason to. The only reason tables have it is because 1) they were added was before foreach loops existed, and 2) because E2 tables are meant to be able to be used with non-sequential keys. E2 arrays can also work with non-sequential keys, but most often you don't want to do that.

If there is no other way than using a foreach-loop, then the following is false (from Expression 2 wiki):

lol
you're just being limited by this other E2 that you say you don't have access to. Blame them for writing bad code.

Well I mean, arrays in E2 aren't meant to be unsequential by design: considering they use default Lua arrays.

Lua doesn't have arrays, only tables, and Lua tables were designed to work for everything. E2 just uses Lua tables to implement arrays (which work best as sequential arrays, but you're not forced to do so), and tables (which are more similar to Lua's tables in the sense that they work for everything - however, E2 tables are a teeny tiny bit slower than E2 arrays due to some additional overhead) completely differently.

Lua doesn't have arrays, only tables, and Lua tables were designed to work for everything. E2 just uses Lua tables to implement arrays (which work best as sequential arrays, but you're not forced to do so), and tables (which are more similar to Lua's tables in the sense that they work for everything - however, E2 tables are a teeny tiny bit slower than E2 arrays due to some additional overhead) completely differently.

I meant that if you used an unsequential array within Lua you're limited because of how the default table functions work, and E2 mainly just "ports" those default functions. Also I said Lua arrays because of how the array part is handled differently internally in Lua.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Anticept picture Anticept  Â·  5Comments

CaptainPRICE picture CaptainPRICE  Â·  9Comments

CaptainPRICE picture CaptainPRICE  Â·  6Comments

TAbdiukov picture TAbdiukov  Â·  5Comments

CaptainPRICE picture CaptainPRICE  Â·  4Comments