简体   繁体   English

VBScript-将循环迭代传递到动态数组中

[英]VBScript - Pass for loop iterations into dynamic array

I have this code which outputs each for loop iteration result into a message box: 我有这段代码将每个for循环迭代结果输出到一个消息框中:

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. 我想将每个迭代结果存储到数组中,然后在消息框中以字符串形式显示完整的数组内容。

I'm trying something like this: 我正在尝试这样的事情:

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

but get Line 6: Error: subscript out of range 'ubound'. 但出现第6行:错误:下标超出范围'ubound'。 Should I be calling the iteration through a function, before mapping it to an array? 在映射到数组之前,是否应该通过函数调用迭代?

(1) UBound() does not fail on an empty array, it it returns -1 (one less as for an array with just one element): (1)UBound()不会在一个空数组上失败,它返回-1(对于只有一个元素的数组,它返回的值少一):

>> 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() 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. (2)UBound()对于a() ()失败,因为a()不是一个空数组,而是一个可憎的东西-一个没有大小的固定数组-编译器/解释器太愚蠢而无法捕捉。 You can't do anything with a() , except overwriting/replacing the variable with something - hopefully - usefull. 您不能使用a()做任何事情,除了用某些东西(希望是有用的a()覆盖/替换变量。

(3) UBound() never returns undefined (there is no undefined in VBScript), but a number. (3)UBound()永远不会返回未定义的(VBScript中没有未定义的),而是一个数字。

(4) Using the loop counter for string positions (1...) and array indices (0...) is misguided; (4)错误地将循环计数器用于字符串位置(1 ...) 数组索引(0 ...); your arrays will contain empty elements at the head. 您的数组的头部将包含空元素。

(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: (5)将字符串元素的计算存储到相应数组中的一种不错的方法是使用有关字符串长度的知识,以避免昂贵的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

Your code is incorrect because UBound fails on empty arrays, it returns undefined. 您的代码不正确,因为UBound在空数组上失败,它返回未定义。

Why don't you use "i" as the index like this (I tested it): 您为什么不使用“ i”这样的索引(我对此进行了测试):

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

Also your syntax is wrong for printing out the array, use something like this: 另外,您的语法对于打印出数组是错误的,请使用以下命令:

for j=1 to i
    WScript.Echo arr(j)
next

If you like another Encryption method using XOR Operator : 如果您喜欢使用XOR运算符的另一种加密方法:

Option Explicit
'Déclaration des variables globales
Dim Titre,MaChaine,fso,ws,LogFile,ChaineCrypt
'Titre du Script
Titre = "Cryptage d'une chaîne de caractères by Hackoo"
Set fso = CreateObject("Scripting.FileSystemObject")
Set ws = CreateObject("Wscript.Shell")
'Nom du fichier qui va stocker le résultat
LogFile = Left(Wscript.ScriptFullName,InstrRev(Wscript.ScriptFullName, ".")) & "txt"
if fso.FileExists(LogFile) Then 'Si le fichier LogFile existe 
    fso.DeleteFile LogFile 'alors on le supprime
end If
'La boîte de saisie de la chaîne de caractères
MaChaine = InputBox("Taper votre chaîne ou bien une phrase pour la crypter",Titre,"www.stackoverflow.com")
'Si la Chaîne est vide ou bien on ne tape rien dans l'inputbox,alors on quitte le script
If MaChaine = "" Then Wscript.Quit 
ChaineCrypt = Crypt(MaChaine,"2015")
MsgBox DblQuote(MaChaine) &" est transformée en "& VbCrlF & VbCrlF & DblQuote(ChaineCrypt),Vbinformation,Titre
Call WriteLog(ChaineCrypt,LogFile)
ws.run LogFile
'************************************************************************
Function Crypt(text,key) 
Dim i,a
For i = 1 to len(text)
      a = i mod len(key)
      if a = 0 then a = len(key)
      Crypt = Crypt & chr(asc(mid(key,a,1)) XOR asc(mid(text,i,1)))
Next
End Function
'*****************************************************************
'Fonction pour ajouter des guillemets dans une variable
Function DblQuote(Str)
    DblQuote = Chr(34) & Str & Chr(34)
End Function
'*****************************************************************
'Fonction pour écrire le résultat dans un fichier texte
Sub WriteLog(strText,LogFile)
    Dim fs,ts 
    Const ForAppending = 8
    Set fs = CreateObject("Scripting.FileSystemObject")
    Set ts = fs.OpenTextFile(LogFile,ForAppending,True)
    ts.WriteLine strText
    ts.Close
End Sub
'*****************************************************************

This is my revised, fully functional code, thanks for the help... 这是我修改后的功能齐全的代码,感谢您的帮助...

dim str, arr, arr2, result, ls, i, encstr, decstr

do while len(str)=0
str = inputbox("Please enter character string for encryption","Cipher")
if isempty(str) then
wscript.quit()
end if
loop

ls=(len(str))
redim arr(ls -1)
for i=1 to ls
arr(i-1) = chr(asc(mid(str,i,1)) - (i-1))
next
encstr = join(arr,"")
rem wscript.echo encstr

ls=len(encstr)
redim arr2(ls-1)
for i=1 to ls
arr2(i-1) = chr(asc(mid(encstr,i,1)) + (i-1))
next
decstr=join(arr2,"")
rem wscript.echo decstr

result=msgbox("Encrypted string: " & encstr & vbcrlf & "Decrypted string: " & decstr,0,"Cipher")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM