如何循环使用一个2数组...下一个循环? [英] How to loop a 2 array using a for each... next loop?

查看:166
本文介绍了如何循环使用一个2数组...下一个循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 For Each ... Next Loop

< (2)
汽车(0)=沃尔沃
汽车(1)=萨博
汽车(2)<%
)=宝马

对于每个x在车中
response.write(x&< br>)
下一个
%>

输出:

  Volvo 
Saab
BMW



<



例如:

我想问一下如何循环的帮助。 (2),水果(2)

汽车(0)=沃尔沃
汽车(1)=萨博
汽车(2)=宝马

水果(0)=苹果
水果(1)=橙色
fruit(2)=Banana

我的期望输出是:

$ b宝马香蕉
$ / code
$ b <前一类=lang-none prettyprint-override> 沃尔沃苹果
萨博橙色

请考虑2数组与它的数字匹配(例如,沃尔沃和苹果是(0)



我试图在互联网上搜索这个,但没有这个主题。对答案大加赞赏。 使用对而不是每个并在两个数组中以相同的索引输出数组。你必须选择其中一个数组来使用 UBound() off,只是让这两个数组具有相同数量的元素,否则你将以 Sub Script超出范围错误。


$ b

  =数组(沃尔沃,萨博,宝马)
暗淡的水果:水果=阵列(苹果,橙色,香蕉)

í我:我= 0

For i = 0 To UBound(cars)
Call Response.Write(cars(i)&& fruits(i))
Next

输出:

 沃尔沃苹果
绅宝橙
宝马香蕉

避免 Sub Script超出范围错误的问题的另一种方法是使用多维数组。



< (2,1)
Dim i:i = 0

things(0,0)= 沃尔沃
东西(0,1)=苹果
东西(1,0)=萨博(2,0)=宝马
东西(2,1)=香蕉

对于i = 0给UBound(东西,1)
调用Response.Write(things(x,0)& & (x,1))
下一个

输出:

<前一类=lang-none prettyprint-override> 沃尔沃苹果
萨博橙色
宝马香蕉

另一种方法是前两个的几乎一个合并,它使用了一个一维数组,但在其内部巢一维数组。


(2)


$
$ )
(1)= Array(Saab,Orange)
(2)= Array(BMW,Banana)


中调用Response.Write(thing(0)&& thing(1))
Next


$ b $输出:
$ b $ prevoise =lang-none prettyprint-override> Volvo Apple
Saab橙色
宝马香蕉


I have this simple basic code of VBScript using For Each...Next Loop

<%
Dim cars(2)
cars(0)="Volvo"
cars(1)="Saab"
cars(2)="BMW"

For Each x In cars
   response.write(x & "<br>")
Next
%>

Output:

Volvo
Saab
BMW

I would like to ask for a help on how to loop if you have two array.

For example:

Dim cars(2), fruits(2)

cars(0)="Volvo"
cars(1)="Saab"
cars(2)="BMW"

fruits(0)="Apple"
fruits(1)="Orange"
fruits(2)="Banana"

What my expectation output is:

Volvo Apple
Saab Orange
BMW Banana

Please also consider that the 2 array matches its number, (e.g Volvo and Apple is both (0) )

I tried to search for this on the internet but no topic for this one. A big appreciation for the answer.

解决方案

Use a For instead of For Each and output the array at the same index in both arrays. You have to pick one of the arrays to use UBound() off, just make surwe both arrays have the same number of element or you will end up with Sub Script out of Range errors.

Dim cars: cars = Array("Volvo", "Saab", "BMW")
Dim fruits: fruits = Array("Apple", "Orange", "Banana")

Dim i: i = 0

For i = 0 To UBound(cars)
  Call Response.Write(cars(i) & " " & fruits(i))
Next

Output:

Volvo Apple
Saab Orange
BMW Banana

Another approach which avoids the issues with Sub Script out of Range errors is to use a Multi Dimensional Array.

Dim things(2, 1)
Dim i: i = 0

things(0, 0) = "Volvo"
things(0, 1) = "Apple"
things(1, 0) = "Saab"
things(1, 1) = "Orange"
things(2, 0) = "BMW"
things(2, 1) = "Banana"

For i = 0 To UBound(things, 1)
  Call Response.Write(things(x, 0) & " " & things(x, 1))
Next

Output:

Volvo Apple
Saab Orange
BMW Banana

Another approach is almost an amalgamation of the first two, it uses a Single Dimension Array but nests Single Dimension Arrays inside it.

Dim things(2)

things(0) = Array("Volvo", "Apple")
things(1) = Array("Saab", "Orange")
things(2) = Array("BMW", "Banana")

For Each thing In things
  Call Response.Write(thing(0) & " " & thing(1))
Next

Output:

Volvo Apple
Saab Orange
BMW Banana

这篇关于如何循环使用一个2数组...下一个循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆