#1

Hi, Is it possible to check if an array contains a certain number without looping through the elements?
Reply
#2

No. Why though? Loops are fast enough.

Use break; once you find the number.
Reply
#3

Quote:
Originally Posted by SickAttack
Посмотреть сообщение
No. Why though? Loops are fast enough.

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

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

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

I think container data types like vector is possible by using this plugin.
https://sampforum.blast.hk/showthread.php?tid=238844
Reply
#6

Quote:
Originally Posted by Sreyas
Посмотреть сообщение
I think container data types like vector is possible by using this plugin.
https://sampforum.blast.hk/showthread.php?tid=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.
Reply
#7

Quote:
Originally Posted by SickAttack
Посмотреть сообщение
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.
Reply
#8

Quote:
Originally Posted by Sreyas
Посмотреть сообщение
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.
Reply
#9

Quote:
Originally Posted by SickAttack
Посмотреть сообщение
Then you should have posted something in pawn.
Huh I believe that plugin allows users to implement vectors in pawn.
Reply
#10

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.

PHP код:
#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.
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)