VBScript - pass loop iterations to a dynamic array

I have this code that outputs each for the loop iteration result in a message field:

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 save each iteration result into an array, and then display the contents of the full array in the message field as a string.

I am 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: index out of range "ubound". Do I have to iterate through a function before matching it with an array?

+1
source share
4 answers

(1) UBound () does not work in an empty array, it returns -1 (one less than for an array with 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() , - , - / , . a(), , / - - , .

(3) UBound() undefined ( VBScript undefined), .

(4) (1...) (0...) ; .

(5) , , ReDim:

>> 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
+2

, UBound , undefined.

"i" , ( ):

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

, - :

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

If you like another encryption method using the XOR operator :

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
'*****************************************************************
+1
source

This is my redesigned, 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")
0
source

All Articles