的VBScript - 通过循环迭代到动态数组 [英] VBScript - Pass for loop iterations into dynamic array

查看:120
本文介绍了的VBScript - 通过循环迭代到动态数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的code,每个循环迭代结果输出到一个消息框:

I have this code which outputs each for loop iteration result into a message box:

dim str

str = inputbox("Please enter character string for encryption","Encryption")

for i=1 to len(str)
wscript.echo asc(mid(str,i,1)) - (i-1)  
next

我想每个迭代结果存储到一个数组,然后显示在消息框中的全部内容数组作为字符串。

I would like to store each iteration result into an array, and then display the full array content in a message box as a string.

我想是这样的:

dim str, arr()

str = inputbox("Please enter character string for encryption","Encryption")

for i=1 to len(str)
redim preserve arr(ubound(arr)+1)
arr(ubound(arr)) = asc(mid(str,i,1)) - (i-1)    
next

wscript.echo arr

但得到6号线:错误:
 下标越界UBOUND。我应该是通过函数调用迭代,将其映射到阵列之前?

but get Line 6: Error: subscript out of range 'ubound'. Should I be calling the iteration through a function, before mapping it to an array?

推荐答案

(1)UBound函数()不会失败一个空数组时,它返回-1(少了一个为只有一个元素的数组):

(1) UBound() does not fail on an empty array, it it returns -1 (one less as for an array with just one element):

>> Dim a : a = Array()
>> WScript.Echo TypeName(a), UBound(a)
>> ReDim a(UBound(a)+1)
>> a(Ubound(a)) = "one and only"
>> WScript.Echo TypeName(a), UBound(a), a(0)
>>
Variant() -1
Variant() 0 one and only

(2)UBound函数()失败 A(),因为 A()不是一个空数组但所憎恶 - 固定数组没有大小 - 编译器/间preTER是太愚蠢赶上。你不能做任何一个(),除了覆盖/的东西取代了变量 - 希望 - 有用

(2) UBound() fails for a(), because a() is not an empty array but an abomination - a fixed array with no size - that the compiler/interpreter is too stupid to catch. You can't do anything with a(), except overwriting/replacing the variable with something - hopefully - usefull.

(3)UBound函数()永远不会返回undefined(有VBScript中没有未定义),但一些。

(3) UBound() never returns undefined (there is no undefined in VBScript), but a number.

(4)使用字符串位置循环计数器(1 ...)数组的索引(0 ...)是错误的;你的阵列将包含头空元素。

(4) Using the loop counter for string positions (1...) and array indices (0...) is misguided; your arrays will contain empty elements at the head.

(5),以字符串元素的计算存储到一个相应的数组体面的方式是使用关于字符串的长度的知识,以避免昂贵的使用ReDim preserve:

(5) The decent way to store computations of string elements into a corresponding array is to use the knowledge about the string's length to avoid the costly ReDim Preserve:

>> s = "abc"
>> ReDim a(Len(s) - 1)
>> For p = 1 To Len(s) : a(p - 1) = Chr(Asc(Mid(s, p, 1)) - 32) : Next
>> WScript.Echo Join(a), Join(a, "")
>>
A B C ABC

这篇关于的VBScript - 通过循环迭代到动态数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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