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)
Function isSetArray(anArray, Optional minSize As Integer = 1) As Boolean
Dim myArray(), anything
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
* Pops the last element out of the array and returns it
- the array passed as an argument loose this element
* adds 'avalue' as a new element at the end of the array
Function arrayAdd(anArray(), aValue)
* Merges the 2 arrays in the 1st one and returns it
Function arrayMerge(Array1(), ByVal Array2)
* Like the 'Join' function but with more possibilities
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
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
* 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)
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:
arrayAdd myArray, newValue
To change this and be able to pass anything (!), you may remove the '()' in the declaration:
- v0.0.2. Improvements to isSetArray, arrayMerge, implode & arraySplice. Addition of explode.
- Testing Sub() procedure at the bottom of the module.
Edited by Djé, 20 June 2006 - 09:59 AM.
Those who do not learn from History are doomed to repeatedly fail their History exams.