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.
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'. 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):
(2) UBound() fails for
a()
, becausea()
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 witha()
, except overwriting/replacing the variable with something - hopefully - usefull.(3) UBound() never returns undefined (there is no undefined in VBScript), but a number.
(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) 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:
Your code is incorrect because UBound fails on empty arrays, it returns undefined.
Why don't you use "i" as the index like this (I tested it):
Also your syntax is wrong for printing out the array, use something like this:
This is my revised, fully functional code, thanks for the help...
If you like another Encryption method using XOR Operator :