Results 1 to 4 of 4

Thread: VBScript Array Push

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Sep 2002
    Location
    Durham, NC, US
    Posts
    218

    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)

  2. #2
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538
    VB Code:
    1. Function Push(ByRef mArray, ByVal mValue)
    2.     Dim intCounter
    3.     Dim intElementCount
    4.  
    5.     If IsArray(mArray) Then
    6.       If IsArray(mValue) Then
    7.         ' Set ubound value once only
    8.         intElementCount = CInt(UBound(mArray))
    9.         ReDim Preserve mArray(CInt(intElementCount + UBound(mValue)))
    10.        
    11.         ' Using for...next is more efficient & quicker than for..each
    12.         For intCounter = 0 To UBound(mValue)
    13.           mArray(intElementCount + intCounter) = mArray(intCounter)
    14.         Next intCounter
    15.       Else
    16.         ReDim Preserve mArray(UBound(mArray) + 1)
    17.         mArray(UBound(mArray)) = mValue
    18.       End If
    19.     Else
    20.       If IsArray(mValue) Then
    21.         mArray = mValue
    22.       Else
    23.         mArray = Array(mValue)
    24.       End If
    25.     End If
    26.     Push = UBound(mArray)
    27.   End Function

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  3. #3
    New Member
    Join Date
    Jun 2006
    Posts
    1

    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:
    1. Function Push(ByRef mArray, ByVal mValue)
    2.   Dim intCounter
    3.   Dim intElementCount
    4.  
    5.   If IsArray(mArray) Then
    6.     If IsArray(mValue) Then
    7.       ' Set ubound value once only
    8.        intElementCount = UBound(mArray)
    9.       ReDim Preserve mArray(intElementCount + UBound(mValue) + 1)
    10.  
    11.       ' Using for...next is more efficient & quicker than for..each
    12.       For intCounter = 0 to UBound(mValue)
    13.         mArray(intElementCount + intCounter + 1) = mArray(intCounter)
    14.       Next
    15.     Else
    16.       ReDim Preserve mArray(UBound(mArray) + 1)
    17.       mArray(UBound(mArray)) = mValue
    18.     End If
    19.   Else
    20.     If IsArray(mValue) Then
    21.       mArray = mValue
    22.     Else
    23.       mArray = Array(mValue)
    24.     End If
    25.   End If
    26.  
    27.   Push = UBound(mArray)
    28. End Function

  4. #4
    New Member
    Join Date
    Nov 2008
    Posts
    1

    Resolved 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
  •  



Click Here to Expand Forum to Full Width