#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2013
    Posts
    4
    Rep Power
    0

    Mapping a string to an array using Split


    Hi All

    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 .

    Dim row
    Dim IntArr()
    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.WaitForString ">"
    crt.Screen.Send ("show configuration interface "& IntArr(i) & " | no-more" & VbCr)

    .....some commands have been omitted from here....

    next
  2. #2
  3. No Profile Picture
    Stumpier old Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jun 2003
    Posts
    14,409
    Rep Power
    4538
    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.
    ======
    Doug G
    ======
    It is a truism of American politics that no man who can win an election deserves to. --Trevanian, from the novel Shibumi
  4. #3
  5. Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jan 2004
    Location
    New Springfield, OH
    Posts
    1,178
    Rep Power
    1469
    IntArr is not a dynamic array.

    Try:

    Dim IntArr
    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.
    Don't like me? Click it.

    Scripting problems? Windows questions? Ask the Windows Guru!

    Stay up to date with all of my latest content. Follow me on Twitter!

    Help us help you! Post your exact error message with these easy tips!
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2013
    Posts
    4
    Rep Power
    0
    Hi Guys;

    Thanks for your help, appreciated.

    my code is actually working now after removing the "()" in the second line, so it became now :

    Dim row
    Dim IntArr

    rather than:

    Dim row
    Dim IntArr()

    appreciate if someone can explain the difference between both though...

    Thanks
  8. #5
  9. Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jan 2004
    Location
    New Springfield, OH
    Posts
    1,178
    Rep Power
    1469
    Originally Posted by mizo1510
    Hi Guys;

    Thanks for your help, appreciated.

    my code is actually working now after removing the "()" in the second line, so it became now :

    Dim row
    Dim IntArr

    rather than:

    Dim row
    Dim IntArr()

    appreciate if someone can explain the difference between both though...

    Thanks
    It goes back to my reply. When you use the parenthesis in your Dim statement, you declare a fixed width array.

    Dim arrSample(5) has 5 nodes.

    By leaving it empty, you're essentially doing the same as this:

    Dim arrSample(null)

    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.
    Don't like me? Click it.

    Scripting problems? Windows questions? Ask the Windows Guru!

    Stay up to date with all of my latest content. Follow me on Twitter!

    Help us help you! Post your exact error message with these easy tips!
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2013
    Posts
    4
    Rep Power
    0
    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
    It goes back to my reply. When you use the parenthesis in your Dim statement, you declare a fixed width array.

    Dim arrSample(5) has 5 nodes.

    By leaving it empty, you're essentially doing the same as this:

    Dim arrSample(null)

    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.
  12. #7
  13. Devshed Beginner (1000 - 1499 posts)

    Join Date
    Jan 2004
    Location
    New Springfield, OH
    Posts
    1,178
    Rep Power
    1469
    Originally Posted by mizo1510
    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...
    The FileSystemObject makes that pretty easy to do. You should go read my article series on our sister site ASP Free. This one should get you started.

    http://www.aspfree.com/c/a/windows-s...-files-in-wsh/
    Don't like me? Click it.

    Scripting problems? Windows questions? Ask the Windows Guru!

    Stay up to date with all of my latest content. Follow me on Twitter!

    Help us help you! Post your exact error message with these easy tips!

IMN logo majestic logo threadwatch logo seochat tools logo