SA-MP Forums Archive
Infinite loop, not sure why though! - 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: Infinite loop, not sure why though! (/showthread.php?tid=437186)



Infinite loop, not sure why though! - Scenario - 14.05.2013

The code below is supposed to look for inline HEX colors inside of a string. In my game-mode, there's "line splitting" so if the size of the string is longer than 144 characters, it will split to a new line. While that works fine, the problem is that if HEX colors were used in the string, they won't carry over to the secondary line. That's what this code was designed to do.

The problem is that it gets into an infinite loop where iStartPosition and iEndPosition remain the same- though that's not what the code should be allowing it to do.

pawn Код:
new
    szLastHexColor[10],
    iStartPosition,
    iEndPosition;

while(strfind(string, "{", false, iEndPosition) != -1)
{
    iStartPosition = strfind(string, "{", false);
    iEndPosition = strfind(string, "}", false);
    strmid(szLastHexColor, string, iStartPosition, iEndPosition+1);
}
Let's take this string for example:

pawn Код:
"[ASSISTANCE] {FFFFFF}- %s(%d) is requesting assistance. Type {FFFF00}/takeas %d {FFFFFF}to take this request!"
Obviously iStartPosition is 13 and iEndPosition is 20. However, with this string, it continues to loop where iStartPosition remains 13 and iEndPosition remains 20.

Any ideas, folks?


Re: Infinite loop, not sure why though! - IceCube! - 14.05.2013

iStartPosition++; at the end of the loop?

Edit I think I'm wrong :/

Edit2: Why shouldn't they stay static? Surelly they should to seperate the string...

Just tring to find out what your doing here...


Re: Infinite loop, not sure why though! - Vince - 14.05.2013

As far as I can see, that code finds the first closing brace over and over again. Specifcally this statement;
pawn Код:
iEndPosition = strfind(string, "}", false);
This always returns the position of the first closing brace because no position parameter is specified. The value of iEndPosition is then once again inserted into the loop condition and the thing starts over again.


AW: Infinite loop, not sure why though! - HurtLocker - 14.05.2013

The problem is that in every loop the 1st { is detected and not the next one after the last found one. Try this:
pawn Код:
new iStartPosition[20], iEndPosition[20];

new szLastHexColor[10],

while(strfind(string, "{", false, iEndPosition[i]) != -1)
{
    iStartPosition[i] = strfind(string, "{", false);
    iEndPosition[i] = strfind(string, "}", false);
    strmid(szLastHexColor, string, iStartPosition, iEndPosition+1);
    strdel(string, iStartPosition[i], iStartPosition[i]);
    strdel(string, iEndPosition[i], iEndPosition[i]);
}
I am not sure how strdel works but the idea is to delete the found { } so that they wont be detected in next scan.


Re: Infinite loop, not sure why though! - Jefff - 14.05.2013

pawn Код:
new
    szLastHexColor[9],
    iStartPosition;

while( (iStartPosition = strfind(string, "{", true, iStartPosition)) != -1)
{
    strmid(szLastHexColor, string, iStartPosition, iStartPosition+8);
    iStartPosition += 8;
}



Re: Infinite loop, not sure why though! - Scenario - 15.05.2013

Quote:
Originally Posted by Jefff
Посмотреть сообщение
pawn Код:
new
    szLastHexColor[9],
    iStartPosition;

while( (iStartPosition = strfind(string, "{", true, iStartPosition)) != -1)
{
    strmid(szLastHexColor, string, iStartPosition, iStartPosition+8);
    iStartPosition += 8;
}
I was just going to post that exact code, basically... I figured it out hours ago, just never had a chance to post my code. Thanks anyways, Jefff!