|
-
Feb 27th, 2004, 11:17 AM
#1
Thread Starter
Addicted Member
VBScript Array Push
Code:
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.
Travis, Kung Foo Journeyman
Web Standards: HTML 4.01, CSS Level 2, ECMA 262 Bindings to DOM Level 1, JavaScript 1.5 Guide and Reference
Perl: Documentation, Learn Perl, Llama, Camel, Cookbook, Perl Monks, Perl Mongers, O'Reilly's Perl.com, ActiveState, CPAN, TPJ, and use Perl;
OSS: Mozilla, MySQL (Manual)
-
Mar 27th, 2004, 08:08 PM
#2
VB Code:
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
-
Jun 19th, 2006, 11:14 PM
#3
New Member
Re: VBScript Array Push
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:
VB Code:
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
-
Nov 12th, 2008, 03:50 PM
#4
New Member
VBScript Array Push With Flattening Option
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.
Code:
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|