All Forums >> [Scripting] >> Post a VBScript >> Example of using Split without giving a variable to the array Do you like VisualBasicScript.com? Link to us and help spread the word about our forum. Thanks!
This is a valid technique to be aware of. I think a different example would show off it's uses better however. In the example you give, you take a static string ("01/08/2005") then convert it to a formatted date-time, split that, and grab the first character of the 3rd field. When all you would really need to do to get the same information is Left("01/08/2005", 1). Again, I don't say this to invalidate the method. Simply to point out that the example does not showcase the method's versatility. Instead, let's say that you have a text file that has a list of users and a share that they map to in this form:
Your job is to echo the name of the server that each user logs in to. (I used an array for the list of lines rather than a text file, but it would work the same regardless)
all you would really need to do to get the same information is Left("01/08/2005", 1)
Not realy. Left("01/08/2005", 1) gave me "0" while my code gave me "A". Try it.
I found your sample interresting too. Especialy the use of For Next and of Function. I don't realy understand why you used a function in this case, but it was interresting to learn how these things work.
Running this code: MsgBox Ucase(Left((Split(FormatDateTime("01/08/2005", 1)," ")(2)),1))
I get a 0 for the result.
As for why I used a function, you are right that it does not ad any value in this instance. I do it mainly out of habit. What usually happens to me is someone says "We want you to take this file and output the name of the server on each line". I do that just fine then 2 days later they say "Oh, and we want to have every 3rd leter capitalized". So I make that change. Then later the same day they say "But only if the 4th letter of the server name is v". So I need to make that change. I just find that keeping things logically grouped lets me handle requests like this better. That is what happens when you are not allowed to gather requirements before you start a project. Keeping things as discrete procedures also helps in a work environment like mine where we encourage everyone to use a common code base. I could then take this function and put it into our script library and everyone could easily copy and paste it into their own scripts at the end and just add one line of code wherever they actually need to use it in their code.
Yes that does give the first leter of the month. My point with the alternative example however was simply that there are built in ways to handle the problem you were presenting without using split. As a matter of fact, I suspect that the reason we ot different results was due to Locale setings. Here is a solution that does not use split and should work for every Locale:
Here ya go. Note that your second question is not possible because the non-OO nature of VBScript prevents the overloading of built in functions. O, and I haven't tested it but I suspect that ChineseAstroAnimal is only valid for AD (or CE) if you prefer) dates.
EDIT: Extra points if you can tell me the significnce of the date I chose. EDIT2: I should have pointed out up front that the ChineseAstroAnimal function is not truly accurate. The chinese zodiac uses a lunar calendaring for determining zodiac. It should however match up fairly accruately with the placemats you find in chinese resturaunts.
< Message edited by ebgreen -- 2/7/2006 5:21:31 AM >