Raised This Month: $ Target: $400
 0% 

Last Array element


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Liverwiz
Veteran Member
Join Date: Feb 2010
Location: Maryland
Old 07-05-2012 , 16:10   Last Array element
Reply With Quote #1

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
__________________
What an elegant solution to a problem that doesn't need solving....

Last edited by Liverwiz; 07-05-2012 at 16:11.
Liverwiz is offline
hleV
Veteran Member
Join Date: Mar 2007
Location: Lithuania
Old 07-05-2012 , 16:12   Re: Last Array element
Reply With Quote #2

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)

Last edited by hleV; 07-05-2012 at 16:14.
hleV is offline
Liverwiz
Veteran Member
Join Date: Feb 2010
Location: Maryland
Old 07-05-2012 , 16:22   Re: Last Array element
Reply With Quote #3

Quote:
Originally Posted by hleV View Post
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.
__________________
What an elegant solution to a problem that doesn't need solving....

Last edited by Liverwiz; 07-05-2012 at 16:23.
Liverwiz is offline
jimaway
Heeeere's Jimmy!
Join Date: Jan 2009
Location: Estonia
Old 07-05-2012 , 16:30   Re: Last Array element
Reply With Quote #4

Code:
new Array:array(1) ArrayPushCell(array, 12) ArrayPushCell(array, 9) ArrayPushCell(array, 10) ArrayPushCell(array, 4) new index = ArraySize(array)
jimaway is offline
Liverwiz
Veteran Member
Join Date: Feb 2010
Location: Maryland
Old 07-05-2012 , 16:31   Re: Last Array element
Reply With Quote #5

Quote:
Originally Posted by jimaway View Post
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
__________________
What an elegant solution to a problem that doesn't need solving....

Last edited by Liverwiz; 07-05-2012 at 16:32.
Liverwiz is offline
jimaway
Heeeere's Jimmy!
Join Date: Jan 2009
Location: Estonia
Old 07-05-2012 , 16:33   Re: Last Array element
Reply With Quote #6

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
if you dont use 0-s as cells values you could use
Code:
new index = (array[charsmax(array)]) ? charsmax(array) : strlen(array)

Last edited by jimaway; 07-05-2012 at 16:42.
jimaway is offline
Liverwiz
Veteran Member
Join Date: Feb 2010
Location: Maryland
Old 07-05-2012 , 16:47   Re: Last Array element
Reply With Quote #7

Quote:
Originally Posted by jimaway View Post
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
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!
__________________
What an elegant solution to a problem that doesn't need solving....

Last edited by Liverwiz; 07-05-2012 at 16:48.
Liverwiz is offline
jimaway
Heeeere's Jimmy!
Join Date: Jan 2009
Location: Estonia
Old 07-05-2012 , 16:52   Re: Last Array element
Reply With Quote #8

doesent have to be chars, but the values have to be 0 < n < 128 i think
jimaway is offline
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 07-05-2012 , 16:55   Re: Last Array element
Reply With Quote #9

Code:
stock GetLastIndex(const array[], size) {     for( new i = 0; i < size; i++ )     {         if( !array[ i ] )         {             return i - 1;         }     }         return size - 1; }
__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!
Exolent[jNr] is offline
Liverwiz
Veteran Member
Join Date: Feb 2010
Location: Maryland
Old 07-05-2012 , 17:07   Re: Last Array element
Reply With Quote #10

Quote:
Originally Posted by jimaway View Post
doesent have to be chars, but the values have to be 0 < n < 128 i think
OH! ok!

Quote:
Originally Posted by Exolent[jNr] View Post
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?
__________________
What an elegant solution to a problem that doesn't need solving....
Liverwiz is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


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


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