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 [url="http://www.msfn.org/board/index.php?showtopic=73144"]Excel ProgsLists generator[/url].
'-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.
[size=2]
Content:[/size]
'* Returns true if the array has at least the specified number of elements (default to one element)
[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[/quote]
* 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
* Merges the 2 arrays in the 1st one and returns it
* Like the 'Join' function but with more possibilities
* Like the 'Split' function but actually working with line breaks!
- Try: Split(myString, vbcrlf) -> error
* 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)
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[/quote]
To change this and be able to pass anything (!), you may remove the '()' in the declaration:
[quote]Function arrayAdd(anArray
[s]()[/s], aValue)[/quote]
[Edits]:
- v0.0.2. Improvements to isSetArray, arrayMerge, implode & arraySplice. Addition of explode.
- Testing Sub() procedure at the bottom of the module.
This post has been edited by Djé: 20 June 2006 - 09:59 AM