AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   retrieve a value from an array and find its position in that array (https://forums.alliedmods.net/showthread.php?t=93661)

moosewanted 05-31-2009 13:29

retrieve a value from an array and find its position in that array
 
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

Bugsy 05-31-2009 13:36

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

Originally Posted by moosewanted (Post 838971)
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;



moosewanted 05-31-2009 13:37

Re: retrieve a value from an array and find its position in that array
 
sigh, I was going to do this but I thought there must be some function already made
Edit: Whoops. Thanks <3

moosewanted 05-31-2009 13:42

Re: retrieve a value from an array and find its position in that array
 
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.

Bugsy 05-31-2009 13:43

Re: retrieve a value from an array and find its position in that array
 
I made a typo and fixed the function. It should be break and not continue.


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

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