Hi people.
I've recently seen other implementations of a function which just checks if a given value (numeric or string) is member on an array. It feels strange to me since I've always thought the best and most straight-forward way for doing that is via the implementation I've always done, which works for both numeric and string variables (please don't mind the .@i inicialization to 0, I know it's useless but it just does reassure me):
// Tries to find if a given element is inside a selected array// @params: element, array// @returns: 1 if found, 0 otherwisefunction in_array { .@total = getarraysize(getarg(1)); .@i = 0; while (.@i < .@total) { if (getarg(0) == getelementofarray(getarg(1),.@i)) { return 1; } .@i++; } return 0;}
But I've recently seen @@Winterfox's implementation in a request's topic (this one), which is a rather creative two-line implementation (disregarding the other lines):
function script PVPLP_revertLevel { setarray .@maps$, "izlude", "pvp2", "pvp3"; getmapxy( .@map$, .@x, .@y, 0 ); if( !compare( implode( .@maps$, ":" ), .@map$ ) ) end; atcommand "@blvl " + (PVPLP_TRUE_LEVEL - BaseLevel);}
Which makes me wonder about what one is more efficient and scalable. I guess Winterfox's implementation is somewhat faster (an implicit loop inside of implode and a string comparison is faster than a loop in which you compare elements one by one), but I've already used mine to find the index of the array which contains a given value (change return 1; to return i; and return 0; to return -1; (for not found) along with function's description (I named it array_pos, by the way).
P.S.: I guess it'd be a good idea if we made a suggestion for getting these functions (in_array and array_pos, whichever its implementation is) inside our repository? Implemented via source or as function, but we should also get repository public functions documented since no one I know makes use of them, or even gets them rewritten to make exactly_the_same_thing.
I've recently seen other implementations of a function which just checks if a given value (numeric or string) is member on an array. It feels strange to me since I've always thought the best and most straight-forward way for doing that is via the implementation I've always done, which works for both numeric and string variables (please don't mind the .@i inicialization to 0, I know it's useless but it just does reassure me):
// Tries to find if a given element is inside a selected array// @params: element, array// @returns: 1 if found, 0 otherwisefunction in_array { .@total = getarraysize(getarg(1)); .@i = 0; while (.@i < .@total) { if (getarg(0) == getelementofarray(getarg(1),.@i)) { return 1; } .@i++; } return 0;}
But I've recently seen @@Winterfox's implementation in a request's topic (this one), which is a rather creative two-line implementation (disregarding the other lines):
function script PVPLP_revertLevel { setarray .@maps$, "izlude", "pvp2", "pvp3"; getmapxy( .@map$, .@x, .@y, 0 ); if( !compare( implode( .@maps$, ":" ), .@map$ ) ) end; atcommand "@blvl " + (PVPLP_TRUE_LEVEL - BaseLevel);}
Which makes me wonder about what one is more efficient and scalable. I guess Winterfox's implementation is somewhat faster (an implicit loop inside of implode and a string comparison is faster than a loop in which you compare elements one by one), but I've already used mine to find the index of the array which contains a given value (change return 1; to return i; and return 0; to return -1; (for not found) along with function's description (I named it array_pos, by the way).
P.S.: I guess it'd be a good idea if we made a suggestion for getting these functions (in_array and array_pos, whichever its implementation is) inside our repository? Implemented via source or as function, but we should also get repository public functions documented since no one I know makes use of them, or even gets them rewritten to make exactly_the_same_thing.
Last edited by a moderator: