Wintermute Engine > Scripts, plugins, utilities, goodies
Array methods
(1/1)
MrBehemoth:
I've made some custom methods for working with arrays. They're mostly pretty simple, but handy to have in the toolbox. It's nice to share, so here they are! (Code below.)
ArraySplice(InputArray, SpliceIndex, DeleteLength, InsertArray) Removes and inserts elements in an array and returns a copy of it. InputArray Array The array to be modified SpliceIndex Int The index of the first element to be deleted and the point to insert new elements DeleteLength Int The number of elements to be deleted, including the element at SpliceIndex InsertArray Array The array to be inserted (optional)ArraySlice(InputArray, SliceIndex, SliceLength) Returns a portion of an array. InputArray Array The array to be extracted from SpliceIndex Int The index of the first element to be returned DeleteLength Int The number of elements to be returned, including the element at SliceIndexArrayConcat(InputArray1, InputArray2) Returns an array made by joining two arrays together. InputArray1 Array The first array to join InputArray2 Array The second array to joinArrayReverse(InputArray) Returns an array that is the input array in reverse order. InputArray Array The array to be reversedArraySort(InputArray, Descending) Sorts a numeric array and returns a copy of it. InputArray Array The array to sort. Must contain only numbers or integers Descending Boolean If true, the sort will be from highest to lowest (optional)ObjectArraySort(InputArray, Descending, Renumber) Sorts an array consisting of objects. InputArray Array The array to sort. Must contain only objects, which must all have a property "Index"This was made for a specific purpose. Descending Boolean If true, the sort will be from highest to lowest (optional)You may or may not find it useful! Renumber Boolean If true, the objects' "Index" properties will be updated to reflect the new order (optional)
And finally, here's all the code:
--- Code: WME Script ---method ArraySplice(InputArray,SpliceIndex,DeleteLength,InsertArray){ var ReturnArray = new Array(); var i; if(DeleteLength == null) DeleteLength = 0; for(i = 0; i < SpliceIndex; i = i + 1) { ReturnArray.Push(InputArray[i]); } if(InsertArray != null) { for(i = 0; i < InsertArray.Length; i = i + 1) { ReturnArray.Push(InsertArray[i]); } } for(i = SpliceIndex + DeleteLength; i < InputArray.Length; i = i + 1) { ReturnArray.Push(InputArray[i]); } return ReturnArray;} method ArraySlice(InputArray,SliceIndex,SliceLength){ var ReturnArray = new Array(); var i; for(i = SliceIndex; i < SliceIndex + SliceLength; i = i + 1) { ReturnArray.Push(InputArray[i]); } return ReturnArray;} method ArrayConcat(InputArray1,InputArray2){ var ReturnArray = new Array(); var i; for(i = 0; i < InputArray1.Length; i = i + 1) { ReturnArray.Push(InputArray1[i]); } for(i = 0; i < InputArray2.Length; i = i + 1) { ReturnArray.Push(InputArray2[i]); } return ReturnArray;} method ArrayReverse(InputArray){ var ReturnArray = new Array(); var i; for(i = InputArray.Length - 1; i >= 0; i = i - 1) { ReturnArray.Push(InputArray[i]); } return ReturnArray;} method ArraySort(InputArray,Descending){ var ReturnArray = new Array(); var i; var ii; var InsertPos; var InsertArray; ReturnArray.Push(InputArray[0]); for(i = 1; i < InputArray.Length; i = i + 1) { InsertPos = -1; for(ii = ReturnArray.Length - 1; ii >= 0 ; ii = ii - 1) { if(InputArray[i] > ReturnArray[ii]) { InsertPos = ii + 1; break; } } InsertArray = new Array(); InsertArray.Push(InputArray[i]); if(InsertPos > -1) ReturnArray = ArraySplice(ReturnArray,InsertPos,0,InsertArray); else ReturnArray = ArrayConcat(InsertArray,ReturnArray); } if(Descending) ReturnArray = ArrayReverse(ReturnArray); return ReturnArray;} method ObjectArraySort(InputArray,Descending,Renumber){ var ReturnArray = new Array(); var i; var ii; var InsertPos; var InsertArray; var InputObject; var ReturnObject; ReturnArray.Push(InputArray[0]); for(i = 1; i < InputArray.Length; i = i + 1) { InsertPos = -1; InputObject = InputArray[i]; for(ii = ReturnArray.Length - 1; ii >= 0 ; ii = ii - 1) { ReturnObject = ReturnArray[ii]; if(InputObject.Index > ReturnObject.Index) { InsertPos = ii + 1; break; } } InsertArray = new Array(); InsertArray.Push(InputArray[i]); if(InsertPos > -1) ReturnArray = ArraySplice(ReturnArray,InsertPos,0,InsertArray); else ReturnArray = ArrayConcat(InsertArray,ReturnArray); } if(Descending) ReturnArray = ArrayReverse(ReturnArray); if(Renumber) { for(i = 0; i < ReturnArray.Length; i = i + 1) { ReturnObject = ReturnArray[i]; ReturnObject.Index = i; ReturnArray[i] = ReturnObject; } } return ReturnArray;}
Cheers!
piere:
Great post ! Thanks for sharing this ::rock
lacosaweb:
Thank you!!!!
It's great!
Navigation
[0] Message Index
Go to full version