﻿ Array methods
• May 28, 2023, 07:20:41 AM
• Welcome, Guest

News:

IRC channel - server: waelisch.de  channel: #wme (read more)

Pages: [1]

AuthorTopic: Array methods  (Read 7137 times)

0 Members and 1 Guest are viewing this topic.

MrBehemoth

• Lurker
• Karma: 2
• Offline
• Gender:
• Posts: 18
Array methods
« on: January 25, 2014, 03:46:05 PM »

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 SliceIndex ArrayConcat(InputArray1, InputArray2) Returns an array made by joining two arrays together. InputArray1 Array The first array to join InputArray2 Array The second array to join ArrayReverse(InputArray) Returns an array that is the input array in reverse order. InputArray Array The array to be reversed ArraySort(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
1. method ArraySplice(InputArray,SpliceIndex,DeleteLength,InsertArray)
2. {
3.         var ReturnArray = new Array();
4.         var i;
5.         if(DeleteLength == null) DeleteLength = 0;
6.         for(i = 0; i < SpliceIndex; i = i + 1)
7.         {
8.                 ReturnArray.Push(InputArray[i]);
9.         }
10.         if(InsertArray != null)
11.         {
12.                 for(i = 0; i < InsertArray.Length; i = i + 1)
13.                 {
14.                         ReturnArray.Push(InsertArray[i]);
15.                 }
16.         }
17.         for(i = SpliceIndex + DeleteLength; i < InputArray.Length; i = i + 1)
18.         {
19.                 ReturnArray.Push(InputArray[i]);
20.         }
21.         return ReturnArray;
22. }
23.
24. method ArraySlice(InputArray,SliceIndex,SliceLength)
25. {
26.         var ReturnArray = new Array();
27.         var i;
28.         for(i = SliceIndex; i < SliceIndex + SliceLength; i = i + 1)
29.         {
30.                 ReturnArray.Push(InputArray[i]);
31.         }
32.         return ReturnArray;
33. }
34.
35. method ArrayConcat(InputArray1,InputArray2)
36. {
37.         var ReturnArray = new Array();
38.         var i;
39.         for(i = 0; i < InputArray1.Length; i = i + 1)
40.         {
41.                 ReturnArray.Push(InputArray1[i]);
42.         }
43.         for(i = 0; i < InputArray2.Length; i = i + 1)
44.         {
45.                 ReturnArray.Push(InputArray2[i]);
46.         }
47.         return ReturnArray;
48. }
49.
50. method ArrayReverse(InputArray)
51. {
52.         var ReturnArray = new Array();
53.         var i;
54.         for(i = InputArray.Length - 1; i >= 0; i = i - 1)
55.         {
56.                 ReturnArray.Push(InputArray[i]);
57.         }
58.         return ReturnArray;
59. }
60.
61. method ArraySort(InputArray,Descending)
62. {
63.         var ReturnArray = new Array();
64.         var i;
65.         var ii;
66.         var InsertPos;
67.         var InsertArray;
68.         ReturnArray.Push(InputArray[0]);
69.         for(i = 1; i < InputArray.Length; i = i + 1)
70.         {
71.                 InsertPos = -1;
72.                 for(ii = ReturnArray.Length - 1; ii >= 0 ; ii = ii - 1)
73.                 {
74.                         if(InputArray[i] > ReturnArray[ii])
75.                         {
76.                                 InsertPos = ii + 1;
77.                                 break;
78.                         }
79.                 }
80.                 InsertArray = new Array();
81.                 InsertArray.Push(InputArray[i]);
82.                 if(InsertPos > -1) ReturnArray = ArraySplice(ReturnArray,InsertPos,0,InsertArray);
83.                 else ReturnArray = ArrayConcat(InsertArray,ReturnArray);
84.         }
85.         if(Descending) ReturnArray = ArrayReverse(ReturnArray);
86.         return ReturnArray;
87. }
88.
89. method ObjectArraySort(InputArray,Descending,Renumber)
90. {
91.         var ReturnArray = new Array();
92.         var i;
93.         var ii;
94.         var InsertPos;
95.         var InsertArray;
96.         var InputObject;
97.         var ReturnObject;
98.         ReturnArray.Push(InputArray[0]);
99.         for(i = 1; i < InputArray.Length; i = i + 1)
100.         {
101.                 InsertPos = -1;
102.                 InputObject = InputArray[i];
103.                 for(ii = ReturnArray.Length - 1; ii >= 0 ; ii = ii - 1)
104.                 {
105.                         ReturnObject = ReturnArray[ii];
106.                         if(InputObject.Index > ReturnObject.Index)
107.                         {
108.                                 InsertPos = ii + 1;
109.                                 break;
110.                         }
111.                 }
112.                 InsertArray = new Array();
113.                 InsertArray.Push(InputArray[i]);
114.                 if(InsertPos > -1) ReturnArray = ArraySplice(ReturnArray,InsertPos,0,InsertArray);
115.                 else ReturnArray = ArrayConcat(InsertArray,ReturnArray);
116.         }
117.         if(Descending) ReturnArray = ArrayReverse(ReturnArray);
118.         if(Renumber)
119.         {
120.                 for(i = 0; i < ReturnArray.Length; i = i + 1)
121.                 {
122.                         ReturnObject = ReturnArray[i];
123.                         ReturnObject.Index = i;
124.                         ReturnArray[i] = ReturnObject;
125.                 }
126.         }
127.         return ReturnArray;
128. }

Cheers!

piere

• Supporter
• Frequent poster
• Karma: 4
• Offline
• Posts: 301
• Sorry for any bad english in my posts. Game on !
Re: Array methods
« Reply #1 on: January 26, 2014, 08:39:04 AM »

Great post ! Thanks for sharing this
Logged

lacosaweb

• Regular poster
• Karma: 1
• Offline
• Posts: 116
Re: Array methods
« Reply #2 on: February 26, 2014, 03:24:20 PM »

Thank you!!!!

It's great!
Logged
Pages: [1]

Page created in 0.038 seconds with 20 queries.