PDA

Click to See Complete Forum and Search --> : VBScript Array Push


Travis G
Feb 27th, 2004, 11:17 AM
Function Push(ByRef mArray, ByVal mValue)
Dim mValEl

If IsArray(mArray) Then
If IsArray(mValue) Then
For Each mValEl In mValue
Redim Preserve mArray(UBound(mArray) + 1)
mArray(UBound(mArray)) = mValEl
Next
Else
Redim Preserve mArray(UBound(mArray) + 1)
mArray(UBound(mArray)) = mValue
End If
Else
If IsArray(mValue) Then
mArray = mValue
Else
mArray = Array(mValue)
End If
End If
Push = UBound(mArray)
End Function


It is something I cooked up for my ASP application. I got sick of working around not having a push. It will push a single value onto an array, or push an array of values. I haven't hardened it, yet, but let me know what you think.

alex_read
Mar 27th, 2004, 08:08 PM
Function Push(ByRef mArray, ByVal mValue)
Dim intCounter
Dim intElementCount

If IsArray(mArray) Then
If IsArray(mValue) Then
' Set ubound value once only
intElementCount = CInt(UBound(mArray))
ReDim Preserve mArray(CInt(intElementCount + UBound(mValue)))

' Using for...next is more efficient & quicker than for..each
For intCounter = 0 To UBound(mValue)
mArray(intElementCount + intCounter) = mArray(intCounter)
Next intCounter
Else
ReDim Preserve mArray(UBound(mArray) + 1)
mArray(UBound(mArray)) = mValue
End If
Else
If IsArray(mValue) Then
mArray = mValue
Else
mArray = Array(mValue)
End If
End If
Push = UBound(mArray)
End Function

hypert
Jun 20th, 2006, 12:14 AM
If you're trying to do this in VBScript (as I am), there's a minor syntax change ("Next" vs. "Next intCounter". But, I also think there's a bug in how the indexes are calculated.

Here's my version:

Function Push(ByRef mArray, ByVal mValue)
Dim intCounter
Dim intElementCount

If IsArray(mArray) Then
If IsArray(mValue) Then
' Set ubound value once only
intElementCount = UBound(mArray)
ReDim Preserve mArray(intElementCount + UBound(mValue) + 1)

' Using for...next is more efficient & quicker than for..each
For intCounter = 0 to UBound(mValue)
mArray(intElementCount + intCounter + 1) = mArray(intCounter)
Next
Else
ReDim Preserve mArray(UBound(mArray) + 1)
mArray(UBound(mArray)) = mValue
End If
Else
If IsArray(mValue) Then
mArray = mValue
Else
mArray = Array(mValue)
End If
End If

Push = UBound(mArray)
End Function

Level99
Nov 12th, 2008, 03:50 PM
Since this is #1 ranked result from Google for VBScript array push, I think we need a little more content.

First of all, the only function that actually works properly "out of the box" (VBScript compiles, indexing properly) is the first post by Travis G.

I'm guilty of blindly copying and pasting (don't worry guys I'm citing the authors / URL for my purposes :)) and ran into a problem. I really did want to build a multidimensional array with this. I appreciate the extra logic that flattens passed in arrays to maintain one container; however, I'd like to control it. In my usage I just removed that section of the code. A flag would be helpful. This is just a slightly modified version of Travis G's original solution.

Function Push(ByRef mArray, ByVal mValue, blnDoFlatten)
Dim mValEl

If IsArray(mArray) Then
If IsArray(mValue) Then
If blnDoFlatten Then
For Each mValEl In mValue
Redim Preserve mArray(UBound(mArray) + 1)
mArray(UBound(mArray)) = mValEl
Next
Else
Redim Preserve mArray(UBound(mArray) + 1)
mArray(UBound(mArray)) = mValue
End If
End If

Else
If IsArray(mValue) Then
mArray = mValue
Else
mArray = Array(mValue)
End If
End If

Push = UBound(mArray)
End Function