# Mintoris Forum

### Author Topic: Array manipulation  (Read 2935 times)

#### Jesse

• Sr. Member
• Posts: 126
• If life throws a planet at you, pull your ripcord‼
##### Array manipulation
« on: Apr 18, 2011, 09:28 PM »
Have you ever needed to insert an element into an array? Or needed to remove one? Mintoris Basic has a lot of functionality concerning arrays, but these two needs haven't been met yet. So can we get it done anyway? You bet we can‼ We could do it by creating two For Next loops assigning each element to a new array, but thats a very inefficient way to do it. Theres a better way: By transforming an array into an itemized string, we gain the functionality we need to work on it. Then we transform it back into an array.

First, of course, we need to setup our array somewhere in our code, and set a delimiter character…

Dim a\$(2) = "Zero", "One", "Three"
d\$ = Chr\$(255)

I recommend always using a delimiter character. If you truly don't need one, specify it anyway, in a string variable, and assign it to the comma character. The reason for this, is if your app needs change and you must use a delimiter in your Item functions, it will already be there, and all you have to do is change a single string var assignment to a new character instead of searching your code and changing every line that needs one. I tend to use the most obscure character I can find.

The simplified way to insert an element into an array, our example will insert the element "Two" in between One and Three, is like this…

' Simplified
a\$ = ItemJoin\$(a\$(), d\$)
a\$ = ItemInsert\$(a\$, "Two", 2, d\$)
a\$() = ItemSplit\$(a\$, d\$)

There is a more efficient way to do this, but it becomes more complicated and harder to read and maintain…

' More efficient
a\$() = ItemSplit\$(ItemInsert\$(ItemJoin\$(a\$(), d\$), "Two", 2, d\$), d\$)

How this becomes less readable and maintainable is quite obvious, but how is it more efficient? By eliminating two "middle men" variable assignments. The result of the ItemJoin\$() becomes the direct input of the ItemInsert\$(), and its result becomes the direct input of the ItemSplit\$() function, eliminating the need for ItemJoin\$() to assign its result to a\$ and ItemInsert\$() to get its input from a\$, and likewise for the other assignment. Either method is fine, but if your looking to squeeze every ounce of performance from your app, consider the more efficient method.

If you want to test your results, that's an easy one. Just do this afterwards…

List a\$(), a

Now, we have successfully inserted an element into a string array, but what about a numeric array? Well, thanks to Chuck (the creator of Mintoris Basic), we have two new array functions: ArrStr\$() which transforms a numeric array into a string array, and ArrVal() which transforms a string array into a numeric array. So all we need to do is insert an ArrStr\$() function before our process and an ArrVal() function afterwards. Here is our new setup…

Dim a(2) = 0, 1, 3
d\$ = Chr\$(255)

And here are the new methods…

' Simplified
a\$() = ArrStr\$(a())
a\$ = ItemJoin\$(a\$(), d\$)
a\$ = ItemInsert\$(a\$, "2", 2, d\$)
a\$() = ItemSplit\$(a\$, d\$)
a() = ArrVal(a\$())

' More efficient
a() = ArrVal(ItemSplit\$(ItemInsert\$(ItemJoin\$(ArrStr\$(a()), d\$), "2", 2, d\$), d\$))

And to list the results of a numeric array, we simple do this…

List ArrStr\$(a()), a

To remove an item from an array, you can use this same logic, just use ItemRemove\$() instead of ItemInsert\$().

I hope you find this info useful.

jesse
« Last Edit: Apr 18, 2011, 11:07 PM by Chuck »

#### Jesse

• Sr. Member
• Posts: 126
• If life throws a planet at you, pull your ripcord‼
##### Re: Array manipulation
« Reply #1 on: Apr 19, 2011, 01:25 PM »
The same technique can be used to search for and find items in an array, using the ItemLocate() function.

'simplified
a\$ = ItemJoin\$(a\$(), d\$)
a = ItemLocate(a\$, searchfor\$, d\$)
'no need to convert a\$ back to an array

'more efficient
a = ItemLocate(ItemJoin\$(a\$(), d\$), searchfor\$, d\$)

This method will not return partial search results, nor return where the search item should be, assuming the array is sorted. For that functionality one should use something like a Binary Search algorithm. I developed a binary search, as well as a trinary search, routine for a project i was working on. Time permitting, i will recode it as a general purpose include file and post it.

#### Jesse

• Sr. Member
• Posts: 126
• If life throws a planet at you, pull your ripcord‼
##### Re: Array manipulation
« Reply #2 on: Apr 19, 2011, 03:54 PM »
On a side note, be careful using the ItwmJoin\$() and ItemSplit\$() functions in loops. I tested a 1000 element array search using my binary search, and it took about 14 seconds to do 1000 searches on the 1000 element array. I was curious to see how the ItemLocate() function performed, and it took 8-11 seconds to search only ten times instead of 1000. This is the search i used…

s = ItemLocate(ItemJoin\$(ArrStr\$(a())), Str\$(i))

I narrowed it down to the ItemJoin\$() function, and although it is VERY handy, its execution speed is crippled by the resource overhead and amount of work it must perform.