Nested ternary stuff - 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: Nested ternary stuff (
/showthread.php?tid=366508)
Nested ternary stuff -
Misiur - 06.08.2012
Hello. This is example code
pawn Code:
main() {
new src[] = "I liek pancakes|52|tomatoes"; //String parsed by sscanf
new str[5][64], int[5], Float:flo[5]; //Variable holders
new is_str[5], is_int[5]; //Variable type holders
new pattern[5] = "sds"; //Sscanf pattern
//Simple loop checking variable type from pattern, char by char (string, decimal and float only allowed)
for(new i = 0, l = strlen(pattern); i < l; i++) {
switch(pattern[i]) {
case 's': {
is_str[i] = true;
}
case 'i': {
is_int[i] = true;
}
}
}
/*
Equivalent to:
if(is_str[%0]) str[%0];
else {
if(is_int[%0]) int[%0];
else flo[%0];
}
*/
#define VAL(%0) ((is_str[%0]) ? (str[%0]) : ((is_int[%0]) ? (int[%0]) : (flo[%0])))
sscanf(src, pattern, VAL(0), VAL(1), VAL(2), VAL(3), VAL(4));
/*
After preprocessing (pawn.cc -l):
sscanf(src, pattern, ((is_str[0]) ? (str[0]) : ((is_int[0]) ? (int[0]) : (flo[0]))), ((is_str[1]) ? (str[1]) : ((is_int[1]) ? (int[1]) : (flo[1]))), ((is_str[2]) ? (str[2]) : ((is_int[2]) ? (int[2]) : (flo[2]))), ((is_str[3]) ? (str[3]) : ((is_int[3]) ? (int[3]) : (flo[3]))), ((is_str[4]) ? (str[4]) : ((is_int[4]) ? (int[4]) : (flo[4]))));
*/
}
Result:
pawn Code:
niga.pwn(23) : warning 213: tag mismatch
niga.pwn(23) : error 033: array must be indexed (variable "str")
Pawn compiler 3.2.3664 Copyright (c) 1997-2006, ITB CompuPhase
Wat? I thought "{Float,_}:..." in argument list means that all tags are acceptable. Dammit pawn, cooperate with me! (upd: now I know that it accepts only weak tags and Float)
Re: Nested ternary stuff -
Misiur - 21.08.2012
Well, that solved tag mismatch error. However it makes it impossible to pass a string variable (when I override the tag, it interprets string as simple array and requires specified index). Is there way past this (some magical OP codes, or using some additional plugins)?
Re: Nested ternary stuff -
Misiur - 21.08.2012
Ok, thank you very much (started playing with fasm few weeks ago, now it's time learn using emit). Nice catch on the loop thingy, I didn't thought about that. I will share the solution if I can manage to get some results