Description: VB Array Functions.Programming Language: Visual Basic (for Applications, but I guess it works also with VB & VBS).Usage: Have the whole set in a module of your project, or just insert in your code the function(s) that you need (beware of dependancies).VB's handling of arrays is under everything. Especially when compared to other languages.
Those functions will come handy to everyone seeking to make something out VB arrays.
Yes, they are the same as those I'm using in my
Excel ProgsLists generator.
'-Double-check but most of them should work with any array, even not 0-base-indexed (no relevant for VBS).
'-Arrays are normally passed ByRef, so the args are updated by the function.
'-They are fairly tested, but the usual disclaimer apply.
Not all array handling functionnalities of other languages are implemented here. If you have other functions that you'd like to be included in the set, PM me.
Content:'* Returns true if the array has at least the specified number of elements (default to one element)
CODE
Function isSetArray(anArray, Optional minSize As Integer = 1) As Boolean
QUOTE
Dim myArray(), anything
isSetArray(myArray) -->False
isSetArray(anything) -->False
myArray=Array("test")
isSetArray(myArray) -->True
myArray=Array()
isSetArray(myArray) -->False
myArray=Array("a", "b", "c")
isSetArray(myArray, 3) -->true
isSetArray(myArray, 4) -->False
* Drops the first element out of the array and returns it
- in the array, the other elements' index is decreased by one
CODE
Function arrayDrop(anArray())
* Pops the last element out of the array and returns it
- the array passed as an argument loose this element
CODE
Function arrayPop(anArray())
* adds 'avalue' as a new element at the end of the array
CODE
Function arrayAdd(anArray(), aValue)
* Merges the 2 arrays in the 1st one and returns it
CODE
Function arrayMerge(Array1(), ByVal Array2)
* Like the 'Join' function but with more possibilities
CODE
Function implode(anArray(), Optional separator As String = " ", Optional keepEmptyElem As Boolean = False)
* Like the 'Split' function but actually working with line breaks!
- Try: Split(myString, vbcrlf) -> error
CODE
Function explode(theString As String, Optional separator As String = " ", Optional keepEmptyElem As Boolean = False)
* Translates an array containing arrays [like array(i)(j)] to a bidimentional array [like array(i, j)]
- The second dimension depends on the dimension of array(0)
- The array passed as an argument is NOT updated
CODE
Function arrayRect(anArray())
* Similar to Splice in JS: remove/insert some elements in the array
- starts operating at the 'start' position (from 1st element=0)
- removes 'count' elements
- inserts the elements of 'additions', which has to be an array (if passed)
CODE
Function arraySplice(anArray(), start As Integer, Optional count As Integer = 1, Optional additions As Variant)
Restriction: Except for 'isSetArray()' and the second arrays of arrayMerge & arraySplice, the array arguments are passed ByRef so they must be declared as arrays prior to pass them to the functions:
QUOTE
Dim myArray()
arrayAdd myArray, newValue
To change this and be able to pass anything (!), you may remove the '()' in the declaration:
QUOTE
Function arrayAdd(anArray(), aValue)
[Edits]:
- v0.0.2. Improvements to isSetArray, arrayMerge, implode & arraySplice. Addition of explode.
- Testing Sub() procedure at the bottom of the module.