Explode() function?
#1

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.
Reply
#2

Код:
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
Reply
#3

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]);
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)