AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Last Array element (https://forums.alliedmods.net/showthread.php?t=189301)

Liverwiz 07-05-2012 16:10

Last Array element
 
Is there a way to determine the last element in an array?

i.e.
Code:

new array[5]
array[0] = 12
array[1] = 9
array[2] = 10
array[3] = 4

new index = getLastIndex(array)

What would i use to determine what is in the last slot of the array? Or what the last filled index is?
like an EOS for cells.
* preferably without looping through it

hleV 07-05-2012 16:12

Re: Last Array element
 
Are you checking the array's slot which has some data (rather than "0") or just want to get the last slot regardless if it's been used?

To get the last possible array slot:
Code:
#define getLastIndex(%1) (sizeof %1 - 1)

Liverwiz 07-05-2012 16:22

Re: Last Array element
 
Quote:

Originally Posted by hleV (Post 1744118)
Are you checking the array's slot which has some data (rather than "0") or just want to get the last slot regardless if it's been used?

To get the last possible array slot:
Code:
#define getLastIndex(%1) (sizeof %1 - 1)

What you answered is actually another one of my questions. But i was wondering how to get the last slot that actually has data in it.

i.e. in my above code i want 3 as the return
Sorry....forgot to specify.

jimaway 07-05-2012 16:30

Re: Last Array element
 
Code:
new Array:array(1) ArrayPushCell(array, 12) ArrayPushCell(array, 9) ArrayPushCell(array, 10) ArrayPushCell(array, 4) new index = ArraySize(array)

Liverwiz 07-05-2012 16:31

Re: Last Array element
 
Quote:

Originally Posted by jimaway (Post 1744128)
Code:
new Array:array(1) ArrayPushCell(array, 12) ArrayPushCell(array, 9) ArrayPushCell(array, 10) ArrayPushCell(array, 4) new index = ArraySize(array)

Yeah....without using a dynamic array?
Not quite where i'm trying to go with the code, ya know?

EDIT: actually....if i wanted to go that in depth i'd use bitsums instead of arrays

jimaway 07-05-2012 16:33

Re: Last Array element
 
without dynamic array and without looping i think the only way is to have a int that you increace every time you fill a cell of an array

edit: i figured something out :P
if you dont use 0-s as cells values you could use
Code:
new index = (array[charsmax(array)]) ? charsmax(array) : strlen(array)

Liverwiz 07-05-2012 16:47

Re: Last Array element
 
Quote:

Originally Posted by jimaway (Post 1744132)
without dynamic array and without looping i think the only way is to have a int that you increace every time you fill a cell of an array

edit: i figured something out :P
if you dont use 0-s as cells values you could use
Code:
new index = (array[charsmax(array)]) ? charsmax(array) : strlen(array)

either way....wouldn't that code return strlen(array)? because charsmax would give that anyway.....and my array is going to be filled with scalars (enums specifically), not chars


I was wondering if there's a native already for it. if not....i'll fucking make one! :D

jimaway 07-05-2012 16:52

Re: Last Array element
 
doesent have to be chars, but the values have to be 0 < n < 128 i think :D

Exolent[jNr] 07-05-2012 16:55

Re: Last Array element
 
Code:
stock GetLastIndex(const array[], size) {     for( new i = 0; i < size; i++ )     {         if( !array[ i ] )         {             return i - 1;         }     }         return size - 1; }

Liverwiz 07-05-2012 17:07

Re: Last Array element
 
Quote:

Originally Posted by jimaway (Post 1744143)
doesent have to be chars, but the values have to be 0 < n < 128 i think :D

OH! ok!

Quote:

Originally Posted by Exolent[jNr] (Post 1744149)
Code:
stock GetLastIndex(const array[], size) {     for( new i = 0; i < size; i++ )     {         if( !array[ i ] )         {             return i - 1;         }     }         return size - 1; }

Is that an actual stock? or would i have to put that in my code?


All times are GMT -4. The time now is 15:16.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.