SA-MP Forums Archive
Small problem about foreach and strcmp - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: Small problem about foreach and strcmp (/showthread.php?tid=602845)



Small problem about foreach and strcmp - AlexBlack - 14.03.2016

Hi, i have a small problem with this function :

PHP код:
stock CheckServerIp(string[]) {
    foreach(new 
iVirtualServer) if(!strcmp(VirtualServerInfo[i][sIP], string)) return i;
    return -
1;

The problem is that this function always return the first value, for example i have the valid VirtualServer value 1,2,3 and the string that i want to get is 3 but it will return 1, i have done some test commands and it's work perfect, this is my test commands below:
PHP код:
CMD:getip(playeridparams[]) {
    new 
value;
    if(
sscanf(params,"d"value)) return Usage(playerid"/getip [id]");
    return 
SendFormatMessage(playerid, -1"%s"VirtualServerInfo[value][sIP]);

Thanks for reading.


Re: Small problem about foreach and strcmp - Threshold - 14.03.2016

strcmp returns 0 if either of the strings are empty. Are you sure the string actually contains something?


Re: Small problem about foreach and strcmp - AlexBlack - 14.03.2016

Yes i'am sure, my test command work and always return the correct value, and i would like to say that this function don't return 0, it's return the first value of the loop, because i have try to edit my function to

PHP код:
stock CheckServerIp(string[]) { 
    foreach(new 
iVirtualServer) if(!strcmp(VirtualServerInfo[i][sIP], string) && != 0) return i;  // return 1
    
return -1

stock CheckServerIp(string[]) { 
    foreach(new 
iVirtualServer) if(!strcmp(VirtualServerInfo[i][sIP], string) && != && != 1) return i;  // return 2
    
return -1




Re: Small problem about foreach and strcmp - Threshold - 14.03.2016

Okay, what are the contents of "VirtualServerInfo[0][sIP]", "VirtualServerInfo[1][sIP]" and "VirtualServerInfo[2][sIP]" when you run those functions? (An example, not actual IPs)


Re: Small problem about foreach and strcmp - AlexBlack - 14.03.2016

I have try this

PHP код:
CMD:getip0(playeridparams[]) return SendFormatMessage(playerid, -1"%s"VirtualServerInfo[0][sIP]); // correct
CMD:getip1(playeridparams[]) return SendFormatMessage(playerid, -1"%s"VirtualServerInfo[1][sIP]); // correct
CMD:getip2(playeridparams[]) return SendFormatMessage(playerid, -1"%s"VirtualServerInfo[2][sIP]); // correct 
and i have a system to see the IP in dialog and it's correct (working fine), i think this can be provoked by the loop.


Re: Small problem about foreach and strcmp - Konstantinos - 14.03.2016

You can reverse the iterator and it's always good to check if the string is not null before comparing it with another string.

PHP код:
CheckServerIp(string[])
{
    foreach(new 
Reverse(VirtualServer))
    {
        if (!
isnull(VirtualServerInfo[i][sIP]) && !strcmp(VirtualServerInfo[i][sIP], string)) return i;
    }
    return -
1;




Re: Small problem about foreach and strcmp - AlexBlack - 14.03.2016

Quote:
Originally Posted by Konstantinos
Посмотреть сообщение
You can reverse the iterator and it's always good to check if the string is not null before comparing it with another string.

PHP код:
CheckServerIp(string[])
{
    foreach(new 
Reverse(VirtualServer))
    {
        if (!
isnull(VirtualServerInfo[i][sIP]) && !strcmp(VirtualServerInfo[i][sIP], string)) return i;
    }
    return -
1;

Looks like the same. (same problem)


Re: Small problem about foreach and strcmp - AlexBlack - 21.04.2016

Bump