[HELP] Exploding a string to an array of substrs; PHP-alike explode() in Pawn?
#1

Hi there,

I'm looking for some way of exploding a regular string into an array of substrings.

Like You would do in PHP:
Код:
 $string = "some|string|separated|with|a|char";
 $array = explode('|', $string);

 print_r($array);
And it would produce:
Код:
Array
(
  [0] => some
  [1] => string
  [2] => separated
  [3] => with
  [4] => a
  [5] => char
)
I've read the official Pawn Strings manual and didn't find any exact solution.
Tried googling, bugging friendly developers and still there's not much i discovered.

Tried experimenting for myself, but honestly i'm not yet good enough with Pawn to figure:

Код:
function strexplode(const token[], const string[], stringSize) {

	new result[256][64];  // some test values
	new row = 0;

	for(new i=0; i<stringSize; i++) {

		// get local char
		new chr[256];
		strmid(chr, string, i, i + 1, sizeof(chr));

		// jump to next row
		if(strcmp(chr, token, true, sizeof(chr)) == 0) row++;
		else strcat(result[row], chr, sizeof(chr));
	}
	
	return result;
}
And then:
Код:
 new line[2048] = "some|string|separated|with|a|char";
 new result[256][64];
 result = strexplode("|", line, sizeof(line));

 SendClientMessage(0, 0xFFFFFFAA, result[1]);  // some test fields
But i still get gibberish from that (result[1] is equal to the whole line..)

Any clues?


Additionally i don't even know how to debug such things properly; launching SAMP server & client anytime i want to check if slight changes worked is pretty slow, and somehow Linux samp server doesn't want to print() anything to it's stdout

Thanks in advance for any helpfull clues
Reply
#2

Thanks a lot Seif!
You helped me again

Any clues on the STDOUT debug tho?
Reply
#3

Or use sscanf, https://sampwiki.blast.hk/wiki/Sscanf
And here you see how to use it: https://sampwiki.blast.hk/wiki/Fast_Commands#sscanf
Reply
#4

Quote:
Originally Posted by Marcel
Ty for the tip Marcel.
I was told to try sscanf() before, but honestly that's not exactly how i am used to work (as PHP/Ruby/JavaScript programmer).
Westy's strlib and explode() seems perfect
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)