December 3rd, 2013, 11:52 PM
Mapping a string to an array using Split
i am having a problem with my VB script, its the first VB script that i write and i think it could be easy but i am not really sure why its happening.
i am trying to write a simple script that would take a list of router interfaces as a string , map this list to an array and then loop on this array to run some show commands on the router.
i am always getting a run-time error at the line when i use this split function. the error says : type mismatch.. any idea why ? your help is really appreciated...
here is the code i am using .
row = "ge-0/0/0,ge-0/0/1,ge-0/0/2"
IntArr = Split(row , ",")
for i = LBound(IntArr) to UBound(IntArr)
crt.Screen.Send ("show configuration interface "& IntArr(i) & " | no-more" & VbCr)
.....some commands have been omitted from here....
December 4th, 2013, 04:25 PM
If I remember vbscript correctly, I think UBound() and LBound() return null if the array isn't populated with at least one node. Check the results of your split, you're trying to split on a comma but I don't see any commas in the string.
Bartender to Rene Descartes "have another beer?" Descartes: "I think not" and he vanished.
December 4th, 2013, 04:50 PM
IntArr is not a dynamic array.
IntArr = Array()
Then your code should work.
Doug, IIRC, UBound and LBound return 0 on an empty array, don't they?
Last edited by Nilpo; December 4th, 2013 at 04:53 PM.
December 4th, 2013, 05:15 PM
Thanks for your help, appreciated.
my code is actually working now after removing the "()" in the second line, so it became now :
appreciate if someone can explain the difference between both though...
December 4th, 2013, 06:47 PM
It goes back to my reply. When you use the parenthesis in your Dim statement, you declare a fixed width array.
Originally Posted by mizo1510
Dim arrSample(5) has 5 nodes.
By leaving it empty, you're essentially doing the same as this:
See what happened? You unintentionally created an array without any elements. When you try to add elements dynamically later, you have problems. (By the way, I generally only use parenthesis when creating multi-dimensional arrays.) Usually this creates a "Subscript out of Range" error, but since the Split method assigns the entire array at once, you got a type mismatch. VBScript does not consider dynamic arrays and fixed width arrays to be the same type in a comparison.
My code explicitly created a dynamic array using the Array() method. You can add or remove elements to your heart's content.
Your code does the same thing, only implicitly. By declaring your variable with a bare Dim statement, VBScript uses the default Variant data type. That means that VBScript determines the data type based on how the variable is used, not on how it is declared. Since you used the variable like an array, VBScript treated your data like an array. This can be helpful in some regards, but just as in this case, it allowed your code to work even though you it wasn't obvious why.
To confuse things even more, it's not even necessary to use Dim statements at all if you're not using Option Explicit. So leaving them out entirely would work equally well too.
Last edited by Nilpo; December 4th, 2013 at 06:56 PM.
December 4th, 2013, 11:13 PM
Thats interestingly good and useful to me, a bit complext considering this is the first code i write since 10 years and my first ever VBScript code...
i ll have to play a little with those things you mentioned so i can understand it thouroughly... but thanks for your help anyway ...
By theway , if i need to redirect my output to a file that i can retrieve later or even send it to a remote server, any simple straightforward way i can use ?
Thanks in advance...
Originally Posted by Nilpo
December 4th, 2013, 11:21 PM