0

Anyone have a favorite implementation of the standard (e.g. jscript, javascript) array.slice(start,end) function in vbscript?

It seems to be commonly missed (among vbscript programmers anyway) and sharing a good implementation would help. If one doesn't show up, I guess I'll have to answer my own question and write something.

3 Answers 3

3

For completeness, this might be a better version:

Function Slice (aInput, Byval aStart, Byval aEnd)
    If IsArray(aInput) Then
        Dim i
        Dim intStep
        Dim arrReturn
        If aStart < 0 Then
            aStart = aStart + Ubound(aInput) + 1
        End If
        If aEnd < 0 Then
            aEnd = aEnd + Ubound(aInput) + 1
        End If
        Redim arrReturn(Abs(aStart - aEnd))
        If aStart > aEnd Then
            intStep = -1
        Else
            intStep = 1
        End If
        For i = aStart To aEnd Step intStep
            If Isobject(aInput(i)) Then
                Set arrReturn(Abs(i-aStart)) = aInput(i)
            Else
                arrReturn(Abs(i-aStart)) = aInput(i)
            End If
        Next
        Slice = arrReturn
    Else
        Slice = Null
    End If
End Function

This avoids a number of issues with the previous answer:

  1. No consideration of objects in the array
  2. Negative start and end values are allowed; they count backwards from the end
  3. If start is higher than end gives a reversed array subset
  4. The (expensive) redim preserve is not necessary since the array is empty
  5. A defined result is returned (Null) if the input is not an array
  6. Uses the built-in function IsArray instead of string manipulation/comparison on the input
Sign up to request clarification or add additional context in comments.

Comments

1

This is one I've used in the past:

Function Slice(arr, starting, ending)

    Dim out_array

    If Right(TypeName(arr), 2) = "()" Then
        out_array = Array()
        ReDim Preserve out_array(ending - starting)
        For index = starting To ending
            out_array(index - starting) = arr(index)
        Next
    Else
        Exit Function
    End If

    Slice = out_array

End Function

Comments

0
Function Slice(arr, starting, ending)

    Dim out_array

    If Right(TypeName(arr), 2) = "()" Then
        out_array = Array()
        If ending=UBound(arr)+1 Then
            actending=ending-1       
            ReDim Preserve out_array(actending - starting)
            For index = starting To actending
                out_array(index - starting) = arr(index)
            Next
         Else
            ReDim Preserve out_array(ending - starting)
            For index = starting To ending
                out_array(index - starting) = arr(index)
            Next
        End If
    Else
        Exit Function
    End If
    Slice = out_array
End Function

1 Comment

Code only answers are not considered good answers, and are likely to be downvoted and/or deleted because they are less useful to a community of learners. Answers with an explanation are always more helpful. The solution may be obvious to you. To others it may not. Please explain what it does, and how it's different from existing answers. Otherwise this answer may be flagged as Low Quality. Here are some guidelines for How do I write a good answer?. Thanks you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.