在经典的Asp VBScript中迭代数组的最佳方式是什么?

时间:2022-02-03 01:34:36

In the code below

在以下代码

For i = LBound(arr) To UBound(arr)

What is the point in asking using LBound? Surely that is always 0.

使用LBound是什么意思?肯定是0。

5 个解决方案

#1


46  

Why not use For Each? That way you don't need to care what the LBound and UBound are.

为什么不使用它们呢?这样你就不需要关心LBound和UBound了。

Dim x, y, z
x = Array(1, 2, 3)

For Each y In x
    z = DoSomethingWith(y)
Next

#2


16  

There is a good reason to NOT USE For i = LBound(arr) To UBound(arr)

有一个很好的理由不使用i = LBound(arr)到UBound(arr)

dim arr(10) allocates eleven members of the array, 0 through 10 (assuming the VB6 default Option Base).

dim arr(10)分配数组的11个成员,从0到10(假设VB6默认的选项基)。

Many VB6 programmers assume the array is one-based, and never use the allocated arr(0). We can remove a potential source of bugs by using For i = 1 To UBound(arr) or For i = 0 To UBound(arr), because then it is clear whether arr(0) is being used.

许多VB6程序员假定数组是基于一个的,并且从不使用分配的arr(0)。我们可以通过使用For i = 1到UBound(arr)或For i = 0到UBound(arr)来移除潜在的bug源,因为这样就可以清楚地知道是否使用了arr(0)。

For each makes a copy of each array element, rather than a pointer.

对于每个数组元素,都创建一个副本,而不是一个指针。

This has two problems.

这两个问题。

  1. When we try to assign a value to an array element, it doesn't reflect on original. This code assigns a value of 47 to the variable i, but does not affect the elements of arr.

    当我们试图给数组元素赋值时,它不会反映原始元素。该代码为变量i赋值47,但不影响arr的元素。

    arr = Array(3,4,8)
    for each i in arr
         i = 47
    next i
    Response.Write arr(0) '- returns 3, not 47

  2. We don't know the index of an array element in a for each, and we are not guaranteed the sequence of elements (although it seems to be in order.)

    我们不知道数组元素在a中的索引,我们也不保证元素的序列(尽管它看起来是有序的)。

#3


3  

LBound may not always be 0.

LBound可能并不总是0。

Whilst it is not possible to create an array that has anything other than a 0 Lower bound in VBScript, it is still possible to retrieve an array of variants from a COM component which may have specified a different LBound.

虽然不可能在VBScript中创建除0下界之外的任何数组,但是仍然可以从可能指定了不同LBound的COM组件中检索变体数组。

That said I've never come across one that has done anything like that.

也就是说,我从未遇到过做过这样事情的人。

#4


1  

Probably it comes from VB6. Because with Option Base statement in VB6, you can alter the lower bound of arrays like this:

可能来自VB6。因为使用VB6中的选项基语句,可以改变数组的下界,如下所示:

Option Base 1

Also in VB6, you can alter the lower bound of a specific array like this:

在VB6中,您还可以改变特定数组的下界,如下所示:

Dim myArray(4 To 42) As String

#5


1  

I've always used For Each...

我总是用在每一个……

#1


46  

Why not use For Each? That way you don't need to care what the LBound and UBound are.

为什么不使用它们呢?这样你就不需要关心LBound和UBound了。

Dim x, y, z
x = Array(1, 2, 3)

For Each y In x
    z = DoSomethingWith(y)
Next

#2


16  

There is a good reason to NOT USE For i = LBound(arr) To UBound(arr)

有一个很好的理由不使用i = LBound(arr)到UBound(arr)

dim arr(10) allocates eleven members of the array, 0 through 10 (assuming the VB6 default Option Base).

dim arr(10)分配数组的11个成员,从0到10(假设VB6默认的选项基)。

Many VB6 programmers assume the array is one-based, and never use the allocated arr(0). We can remove a potential source of bugs by using For i = 1 To UBound(arr) or For i = 0 To UBound(arr), because then it is clear whether arr(0) is being used.

许多VB6程序员假定数组是基于一个的,并且从不使用分配的arr(0)。我们可以通过使用For i = 1到UBound(arr)或For i = 0到UBound(arr)来移除潜在的bug源,因为这样就可以清楚地知道是否使用了arr(0)。

For each makes a copy of each array element, rather than a pointer.

对于每个数组元素,都创建一个副本,而不是一个指针。

This has two problems.

这两个问题。

  1. When we try to assign a value to an array element, it doesn't reflect on original. This code assigns a value of 47 to the variable i, but does not affect the elements of arr.

    当我们试图给数组元素赋值时,它不会反映原始元素。该代码为变量i赋值47,但不影响arr的元素。

    arr = Array(3,4,8)
    for each i in arr
         i = 47
    next i
    Response.Write arr(0) '- returns 3, not 47

  2. We don't know the index of an array element in a for each, and we are not guaranteed the sequence of elements (although it seems to be in order.)

    我们不知道数组元素在a中的索引,我们也不保证元素的序列(尽管它看起来是有序的)。

#3


3  

LBound may not always be 0.

LBound可能并不总是0。

Whilst it is not possible to create an array that has anything other than a 0 Lower bound in VBScript, it is still possible to retrieve an array of variants from a COM component which may have specified a different LBound.

虽然不可能在VBScript中创建除0下界之外的任何数组,但是仍然可以从可能指定了不同LBound的COM组件中检索变体数组。

That said I've never come across one that has done anything like that.

也就是说,我从未遇到过做过这样事情的人。

#4


1  

Probably it comes from VB6. Because with Option Base statement in VB6, you can alter the lower bound of arrays like this:

可能来自VB6。因为使用VB6中的选项基语句,可以改变数组的下界,如下所示:

Option Base 1

Also in VB6, you can alter the lower bound of a specific array like this:

在VB6中,您还可以改变特定数组的下界,如下所示:

Dim myArray(4 To 42) As String

#5


1  

I've always used For Each...

我总是用在每一个……