The purpose of the patch is to extend the existing array methods API.
Added functions:
array.all() - return 1 if all elements of the array are true, 0 otherwise. Example: {1, "hello"}.all() -> 1, {0, "hello"} -> 0 // because 0 is false
array.any() - return 1 if any element of the array is true, 0 otherwise. Example: {0, 1, "hi"}.any() -> 1, {0, ""} -> 0 // because 0 and "" are both false
array.remove(value) - removes any object found in the array, returns 1 if the object is found, 0 on failure. Example: {0, 1, "hi"}.remove(1) -> 1, the resulting array is {0, "hi"}
array.removeall(value) - same as above, but removes all the occurrences of value found inside the array
array.pop() - removes the last element of the array, returning it. Example: {1, 2, 3}.pop() -> 3, when the array shrinks by one element to {1,2}
array.poprandom() - same as above, but retrieves a random element of the array removing it from the array afterwards.
array.sortby(membername) - sorts an array of objects by their member value, ascending order. Example: {attacker,
defender}.sortby("cmdlevel") will sort mobiles by their .cmdlevel
array.join(sep) - creates a concatenated (joined) string representation of the array contents, joined by sep. Example: {"I", "am",
"a", "warrior", "level", 1}.join(" ") -> "I am a warrior level 1"
array.collect(membername, [membervalue]) - creates new array that includes all element of this array with membername equals membervalue. If membervalue isn't specified then new array will have all the elements for which .membername returns true. Note: this method will *always* remove all uninit objects from the array. Ex: {who, "test"}.collect("name") -> {who} // doesn't include string "name" because it doesn't have .name member.
array.reject(membername, [membervalue]) - creates new array that includes all element of an old array whose membername not equals to membervalue. If membervalue isn't specified then new array will have all the elements for which .membername returns false or non-existent.
PS: Kevin had counseled me on POL intrinsic mechanics throughout the development process. Without him the API would not have been possible.
array extended functions
Moderator: POL Developer
array extended functions
- Attachments
-
- array_extended_api_v2.patch
- POL Array additional methods
- (10.47 KiB) Downloaded 363 times
Last edited by andenixa on Sun Apr 14, 2013 3:07 am, edited 3 times in total.
Re: array extended functions
Is there any error checking with array.sortby() for non-UO objects that might be stored in an array element and array.join() for non-strings stored in an array element?
Re: array extended functions
Hello Yukiko,
As of now objects with inexistent members are being resolved as "uninit". They don't cause any issues, but give somewhat random sorting results.
As of now objects with inexistent members are being resolved as "uninit". They don't cause any issues, but give somewhat random sorting results.
Re: array extended functions
I don't know if it's possible but it might be good to provide error checking for those methods that require specific datatypes in arrays. That way your methods would be consistent with POL's normal operation. As a general rule POL does provide error checking when there are incompatible datatypes. It definitely helps with debugging code if a method/function returns error information.
Anyway, thanks.
Anyway, thanks.
Re: array extended functions
There is an extensive error checking for all the calls that require a specific data type in the array. Just for sorting I don't think its due. Besides I am not sure that the team would ever consider my API anyway.
Re: array extended functions
Why should we not? For me it sounds like a good addition, I am planning to look into it but hadn't the time to do so.
Give me some time
Give me some time
Re: array extended functions
At first glance some of the methods do appear very specialized but I can see the application for others. So I don't see why the developers wouldn't add your patch.
Re: array extended functions
I'd also like to hear if you have any comments about my string.format() call. I consider it to have even better potential. At least at the shard I use it quite frequently. I'd gladly patch any instabilities had they arise.