Explode() function? - 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: Explode() function? (
/showthread.php?tid=310315)
Explode() function? -
xGoldenx - 11.01.2012
Hey
Is there any function in PAWN like explode() in php? I really need it, I want to format IP from for example
127.0.0.1 to
127.0.0.
Re: Explode() function? -
spedico - 11.01.2012
Код:
stock explode(aExplode[][], const sSource[], const sDelimiter[] = " ", iVertices = sizeof aExplode, iLength = sizeof aExplode[])
{
new
iNode,
iPointer,
iPrevious = -1,
iDelimiter = strlen(sDelimiter);
while(iNode < iVertices)
{
iPointer = strfind(sSource, sDelimiter, false, iPointer);
if(iPointer == -1)
{
strmid(aExplode[iNode], sSource, iPrevious, strlen(sSource), iLength);
break;
}
else
{
strmid(aExplode[iNode], sSource, iPrevious, iPointer, iLength);
}
iPrevious = (iPointer += iDelimiter);
++iNode;
}
return iPrevious;
}
By westie
Re: Explode() function? -
AndreT - 11.01.2012
There are several functions like that, yes. Googling showed me a few interesting functions right away, search for name
split. But if you want to go for a better solution (much faster also) that can be used in various situations (database output parsing, command parameter parsing, etc), I suggest you use sscanf (
link).
Although such IP operation can be done by finding the last period mark in the string and deleting anything after it, I suppose you want support for more cases, in which sscanf will help greatly.
pawn Код:
new IP[4];
sscanf(IP, "a<d>[5]", IP);
// if the version above doesn't work, refer to this
sscanf(IP, "p<.>dddd", IP[0], IP[1], IP[2], IP[3]);