Raised This Month: $ Target: $400
 0% 

retrieve a value from an array and find its position in that array


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
moosewanted
Member
Join Date: May 2009
Old 05-31-2009 , 13:29   retrieve a value from an array and find its position in that array
Reply With Quote #1

Hey,
Probably really simple but I can't find any information.

I have a value which is stored in an array, but I want to find out it's position in the array and then use this new number.

Example:
PHP Code:
new g_Array[33]

public 
some_function(id)
{
    
g_Array[id] = 232
}

public 
some_other(array_value)
{
    
//I have the array value 232, but I want to find the id(position) in the array

Thanks for help
moosewanted is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 05-31-2009 , 13:36   Re: retrieve a value from an array and find its position in that array
Reply With Quote #2

Quote:
Originally Posted by moosewanted View Post
Hey,
Probably really simple but I can't find any information.

I have a value which is stored in an array, but I want to find out it's position in the array and then use this new number.

Example:
PHP Code:
new g_Array[33]

public 
some_function(id)
{
    
g_Array[id] = 232
}

public 
some_other(array_value)
{
    
//I have the array value 232, but I want to find the id(position) in the array

Thanks for help
To find and replace a value in an array:
PHP Code:
public ReplaceInArrayiArray[] , iFind iReplace )
{
    for ( new 
sizeof iArray i++ )
    {
        if ( 
iArray[i] == iFind )
        {
            
iArray[i] = iReplace;
            break;
        }
    }
}

To find the index of an item in an array:
PHP Code:
public FindPositioniArray[] , iVal )
{
    for ( new 
sizeof iArray i++ )
        if ( 
iArray[i] == iVal )
            return 
i;

__________________

Last edited by Bugsy; 05-31-2009 at 13:41.
Bugsy is offline
moosewanted
Member
Join Date: May 2009
Old 05-31-2009 , 13:37   Re: retrieve a value from an array and find its position in that array
Reply With Quote #3

sigh, I was going to do this but I thought there must be some function already made
Edit: Whoops. Thanks <3
moosewanted is offline
moosewanted
Member
Join Date: May 2009
Old 05-31-2009 , 13:42   Re: retrieve a value from an array and find its position in that array
Reply With Quote #4

PHP Code:
stock fm_remove_rocket(id)
{
    
engfunc(EngFunc_RemoveEntityid)
    for(new 
033i++)
    {
        if(
g_Rocket[i] == id) break;
        continue;
    }
    
g_HasRocket[i] = false

Have to go eat, don't even know if "break" exists.
moosewanted is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 05-31-2009 , 13:43   Re: retrieve a value from an array and find its position in that array
Reply With Quote #5

I made a typo and fixed the function. It should be break and not continue.
__________________
Bugsy 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 14:01.


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