PDA

View Full Version : Array


GoldenLion
19/12/2016, 02:04 PM
Hi, Is it possible to check if an array contains a certain number without looping through the elements?

SickAttack
19/12/2016, 02:06 PM
No. Why though? Loops are fast enough.

Use break; once you find the number.

GoldenLion
19/12/2016, 02:08 PM
No. Why though? Loops are fast enough.

Use break; once you find the number.
Well I was just wondering. Thanks.

SickAttack
19/12/2016, 02:09 PM
It is possible if you put each number in their index however.

if(array[number] == number]) or even as a boolean.

SyS
19/12/2016, 02:11 PM
I think container data types like vector is possible by using this plugin.
http://forum.sa-mp.com/showthread.php?t=238844

SickAttack
19/12/2016, 02:18 PM
I think container data types like vector is possible by using this plugin.
http://forum.sa-mp.com/showthread.php?t=238844

Comments say it is slower than normal arrays and work like PVars, so I don't see the point of why you should avoid a simple loop.

SyS
19/12/2016, 02:19 PM
Comments say it is slower than normal arrays and work like PVars, so I don't see the point of why you should avoid a simple loop.

The OP just want to know if its possible not that he is going to implement it. There is no harm in asking.

SickAttack
19/12/2016, 02:22 PM
The OP just want to know if its possible not that he is going to implement it. There is no harm in asking.

Then you should have posted something in pawn.

SyS
19/12/2016, 02:24 PM
Then you should have posted something in pawn.

Huh ??? I believe that plugin allows users to implement vectors in pawn.

SickAttack
19/12/2016, 02:29 PM
Huh ??? I believe that plugin allows users to implement vectors in pawn.

Without a plugin, in pawn itself.

SyS
19/12/2016, 02:31 PM
Without a plugin, in pawn itself.

and that's because....

OP never said with or without plugin he just asked its possible or not. We all know its not possible in pawn itself -_-

SickAttack
19/12/2016, 02:38 PM
and that's because....

OP never said with or without plugin he just asked its possible or not. We all know its not possible in pawn itself -_-

It's only logical that he's talking about PAWN, plugins are in a different language. But you may be right in global terms.

Just leave it where it stands, I'm not going to reply anymore either way xD

It is possible if you put each number in their index however.

if(array[number] == number]) or even as a boolean.

SyS
19/12/2016, 02:39 PM
Just leave it where it stands, I'm not going to reply anymore either way xD

No no don't take it in bad way i'm not arguing i was pointing to OP's question only :)

SyS
18/03/2017, 02:15 PM
Sorry for late reply but i found something. Its actually possible. Since pawn is type less we can make use string function strfind to check it . I made a small macro for convenience. Its actually faster than normal looping.But there is a disadvantage if we have an element value equal to 0 the checking stops on that index and further index would not be checked and will return -1.I will try to find a method to overcome this.


#define arrfind(%0,%1) strfind(%0,{%1},false,0)

main()
{
new array[]={6,2,3,3,2340,78};

printf( "index = %d" , arrfind(array,3));//will print 2

}



now there is another way for checking without looping which is recursion but i think normal looping is better than that.

GhostHacker9
18/03/2017, 02:22 PM
Sorry for late reply but i found something. Its actually possible. Since pawn is type less we can make use string function strfind to check it . I made a small macro for convenience. Its actually faster than normal looping.But there is a disadvantage if we have an element value equal to 0 the checking stops on that index and further index would not be checked and will return -1.I will try to find a method to overcome this.


#define arrfind(%0,%1) strfind(%0,{%1},false,0)

main()
{
new array[]={6,2,3,3,2340,78};

printf( "index = %d" , arrfind(array,3));//will print 2

}



now there is another way for checking without looping which is recursion but i think normal looping is better than that.

That's fucking useful and gave me some ideas. Have a rep.