Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: VBS Script...need to know something  (Read 7921 times)

0 Members and 1 Guest are viewing this topic.

thunder708

    Topic Starter


    Rookie

    VBS Script...need to know something
    « on: February 03, 2010, 03:20:11 PM »
    right i have this script and i wonder if theres a way i can make it shorter? ive tried using an array but it dint work, any ideas?

    ill only put part of the script as its too bgi and you will get the gist of it

    Code: [Select]
    dim letter(25)
    dim value
    value = 0

    name=inputbox("Input Your Name to Get a Value:","Input Name")
    name2=LCase(name)
    length = len(name)

    for i = 1 to length
    letter(i) = mid(name2, i, 1)

    if letter(i) = "a" then
    value = value + 1
    end if
    if letter(i) = "b" then
    value = value + 2
    end if
    if letter(i) = "c" then
    value = value + 3
    end if
    if letter(i) = "d" then
    value = value + 4
    end if
    if letter(i) = "e" then
    value = value + 5
    end if


    etc.... untill

    if letter(i) = "z" then

    value = value +26

    msgbox(value)


    so any takers?

    Geek-9pm


      Mastermind
    • Geek After Dark
    • Thanked: 1026
      • Gekk9pm bnlog
    • Certifications: List
    • Computer: Specs
    • Experience: Expert
    • OS: Windows 10
    Re: VBS Script...need to know something
    « Reply #1 on: February 03, 2010, 05:40:23 PM »
    Make a string with all the letters.
    Find the position of the single letter in the string. That number you will add to value. There is a function that finds the start position of a small sting inside of larger string. Avoid null strings.

    Look up VBScript InStr Function

    BC_Programmer


      Mastermind
    • Typing is no substitute for thinking.
    • Thanked: 1140
      • Yes
      • Yes
      • BC-Programming.com
    • Certifications: List
    • Computer: Specs
    • Experience: Beginner
    • OS: Windows 11
    Re: VBS Script...need to know something
    « Reply #2 on: February 03, 2010, 05:42:02 PM »
    the trick is to understand the ASCII codes involved. lowercase A is 97; and each subsequent code up to 122 is another letter. What I've done in the following is take the ascii code of each character, and if it is a alphabetic char (>=97, and smaller then 123) then 97 subtracted from the ascii value is added to the result.

    any other character, such as a exclamation mark (ascii code 33) would result in a negative number being added, thus the check. in those cases the code is simply added as-is. you could make it ignore non-alphabetic characters, though.
    Code: [Select]
    Dim name,value
    Const lowercaseA=97
    name=inputbox("Input Your Name to Get a Value:","Input Name")
    name2=LCase(name)
    length = Len(name)

    for i = 1 to length
    charasc=Asc(Mid(name2,i,1))

    if charasc >= lowercaseA and charasc < lowercaseA+26 then
    value = value + (charasc)-lowercaseA
    else
    value = value + charasc
    end if
    next


    msgbox value
    I was trying to dereference Null Pointers before it was cool.

    Geek-9pm


      Mastermind
    • Geek After Dark
    • Thanked: 1026
      • Gekk9pm bnlog
    • Certifications: List
    • Computer: Specs
    • Experience: Expert
    • OS: Windows 10
    Re: VBS Script...need to know something
    « Reply #3 on: February 03, 2010, 05:46:34 PM »
    the trick is to understand the ASCII codes involved. lowercase A is 97; and each subsequent code up to 122 is another letter. What I've done in the following is take the ascii code of each character, and if it is a alphabetic char (>=97, and smaller then 123) then 97 subtracted from the ascii value is added to the result.

    any other character, such as a exclamation mark (ascii code 33) would result in a negative number being added, thus the check. in those cases the code is simply added as-is. you could make it ignore non-alphabetic characters, though.
    Code: [Select]
    Dim name,value
    Const lowercaseA=97
    name=inputbox("Input Your Name to Get a Value:","Input Name")
    name2=LCase(name)
    length = Len(name)

    for i = 1 to length
    charasc=Asc(Mid(name2,i,1))

    if charasc >= lowercaseA and charasc < lowercaseA+26 then
    value = value + (charasc)-lowercaseA
    else
    value = value + charasc
    end if
    next


    msgbox value
    WRONG!
    He never said it was ASCII. You get two demerits for that!
    InStr makes it a one liner and is not ASCII dependent.

    BC_Programmer


      Mastermind
    • Typing is no substitute for thinking.
    • Thanked: 1140
      • Yes
      • Yes
      • BC-Programming.com
    • Certifications: List
    • Computer: Specs
    • Experience: Beginner
    • OS: Windows 11
    Re: VBS Script...need to know something
    « Reply #4 on: February 03, 2010, 06:03:42 PM »
    WRONG!
    He never said it was ASCII. You get two demerits for that!
    InStr makes it a one liner and is not ASCII dependent.

    Instr doesn't help at all.

    perhaps you should actually run the code and see what it does.

    he's creating a sum of the alphabetic sequence characters. a is 1, b is 2, etc. and they are added up for every character, and the result printed.

    Instr does not even come close to doing any of that.


    Additionally, InStr doesn't work with non-ANSI characters, anyway. otherwise:
    Code: [Select]
    instr("AAAAĆAAA","AE")

    would return 5; but it returns zero. AE and the Ć character are typographically equivalent for any unicode aware comparison, so evidently Instr is not Unicode aware, and since VBScript only runs on windows it's safe to assume that it uses the only other available character set, which is ANSI. the first 127 characters of ANSI are the same as those in ASCII.

    Sure, Visual Basic/VBScript  store All their strings in unicode internally, but Asc("A") will always give you 65. in fact, AscW("A") still gives you 65.
    I was trying to dereference Null Pointers before it was cool.

    Geek-9pm


      Mastermind
    • Geek After Dark
    • Thanked: 1026
      • Gekk9pm bnlog
    • Certifications: List
    • Computer: Specs
    • Experience: Expert
    • OS: Windows 10
    Re: VBS Script...need to know something
    « Reply #5 on: February 03, 2010, 06:37:41 PM »
    Right. Not Unicode. Did I say Unicode?
    I was thinking of extended ASCII.
    Like what some body had the name 'Peńa' ?
     

    BC_Programmer


      Mastermind
    • Typing is no substitute for thinking.
    • Thanked: 1140
      • Yes
      • Yes
      • BC-Programming.com
    • Certifications: List
    • Computer: Specs
    • Experience: Beginner
    • OS: Windows 11
    Re: VBS Script...need to know something
    « Reply #6 on: February 03, 2010, 06:50:11 PM »
    Right. Not Unicode. Did I say Unicode?
    I was thinking of extended ASCII.
    Like what some body had the name 'Peńa' ?
     

    the ASCII standard calls for 8 bits; the older 7-bit constraint hasn't been seen for ages, when EBCDIC  was still prevalent.

    In any case, since they are only counting alphabetic characters, and the alphabetic characters have the same ordinal values in your "basic ASCII" as well as normal 8-bit ascii as well as Unicode I really don't see my method having any issues.
    I was trying to dereference Null Pointers before it was cool.

    Geek-9pm


      Mastermind
    • Geek After Dark
    • Thanked: 1026
      • Gekk9pm bnlog
    • Certifications: List
    • Computer: Specs
    • Experience: Expert
    • OS: Windows 10
    Re: VBS Script...need to know something
    « Reply #7 on: February 03, 2010, 08:51:03 PM »
    You have made the  point.
    I should not have said WRONG.
    I should have said POOR EXAMPLE!
    So your still get one demerit.
    Look over your code and see what you could have done.
    Or what you should have left out.


    BC_Programmer


      Mastermind
    • Typing is no substitute for thinking.
    • Thanked: 1140
      • Yes
      • Yes
      • BC-Programming.com
    • Certifications: List
    • Computer: Specs
    • Experience: Beginner
    • OS: Windows 11
    Re: VBS Script...need to know something
    « Reply #8 on: February 03, 2010, 09:12:51 PM »
    Aside from the constant, there is nothing even remotely ASCII-specific.

    And unlike the OPs code mine doesn't error when you type in a z with "Subscript out of Range"  :P

    I could have left out the extraneous variables but I was trying to keep things consistent with the Original Posters code.

    I should have said POOR EXAMPLE!

    Better then yours. Because you didn't provide one.
    I was trying to dereference Null Pointers before it was cool.

    Geek-9pm


      Mastermind
    • Geek After Dark
    • Thanked: 1026
      • Gekk9pm bnlog
    • Certifications: List
    • Computer: Specs
    • Experience: Expert
    • OS: Windows 10
    Re: VBS Script...need to know something
    « Reply #9 on: February 03, 2010, 09:29:26 PM »
    Quote
    And unlike the OPs code mine doesn't error when you type in a z with "Subscript out of Range"  Tongue
    Really? How did that happen? Are you sure?
                                       

    BC_Programmer


      Mastermind
    • Typing is no substitute for thinking.
    • Thanked: 1140
      • Yes
      • Yes
      • BC-Programming.com
    • Certifications: List
    • Computer: Specs
    • Experience: Beginner
    • OS: Windows 11
    Re: VBS Script...need to know something
    « Reply #10 on: February 03, 2010, 09:39:40 PM »
    Really? How did that happen? Are you sure?
                                       


    Oh, actually, I was reading it wrong... the error would occur if you type in something longer then 25 characters.

    I was trying to dereference Null Pointers before it was cool.

    Geek-9pm


      Mastermind
    • Geek After Dark
    • Thanked: 1026
      • Gekk9pm bnlog
    • Certifications: List
    • Computer: Specs
    • Experience: Expert
    • OS: Windows 10
    Re: VBS Script...need to know something
    « Reply #11 on: February 03, 2010, 10:33:49 PM »
    Oh, actually, I was reading it wrong... the error would occur if you type in something longer then 25 characters.
    Right!
    Now aren't you glad I made you pay attention.
    Code: [Select]
    name2=LCase(name2)
    for i = 1 to length
       charasc = Asc(Mid(name2, i, 1))
       value = value + (charasc) - lowercaseA  + 1
    next

    BC_Programmer


      Mastermind
    • Typing is no substitute for thinking.
    • Thanked: 1140
      • Yes
      • Yes
      • BC-Programming.com
    • Certifications: List
    • Computer: Specs
    • Experience: Beginner
    • OS: Windows 11
    Re: VBS Script...need to know something
    « Reply #12 on: February 04, 2010, 01:48:42 AM »
    ahhh, good catch. Too bad it took 4 posts for you to mention it, heh.
    I was trying to dereference Null Pointers before it was cool.

    thunder708

      Topic Starter


      Rookie

      Re: VBS Script...need to know something
      « Reply #13 on: February 04, 2010, 03:06:43 AM »
      thanks for the help, but when i run this code:

      Code: [Select]
      Dim name,value
      Const lowercaseA=97
      name=inputbox("Input Your Name to Get a Value:","Input Name")
      name2=LCase(name)
      length = Len(name)

      for i = 1 to length
      charasc=Asc(Mid(name2,i,1))

      if charasc >= lowercaseA and charasc < lowercaseA+26 then
      value = value + (charasc)-lowercaseA
      else
      value = value + charasc
      end if
      next

      msgbox value

      when typing in my name - mike - i get a value of 34 when i know with my code i get 38

      also when i use this code

      Code: [Select]
      name2=LCase(name2)
      for i = 1 to length
         charasc = Asc(Mid(name2, i, 1))
         value = value + (charasc) - lowercaseA  + 1
      next

      i get a value of 0 everytime.

      WRONG!
      He never said it was ASCII. You get two demerits for that!
      InStr makes it a one liner and is not ASCII dependent.

      I know i didnt say ASCII but i asked for any help to shorten my code, which i think he has done, so i don't think this was neccessary was it?


      I have made a smiliar program in Small Basic and here is the code

      Code: [Select]
      TextWindow.Title = "Word & Name Valuer By Thunder708"
      l = "a=1;b=2;c=3;d=4;e=5;f=6;g=7;h=8;i=9;j=10;k=11;l=12;m=13;n=14;o=15;p=16;q=17;r=18;s=19;t=20;u=21;v=22;w=23;x=24;y=25;z=26"
      TextWindow.Write("Please Enter A name or word to get a value: ")
      name = textwindow.read()
      TextWindow.WriteLine("")
      tvalue = 0
      TextWindow.WriteLine("Letter   Value")
      TextWindow.WriteLine("")
      For i = 1 To Text.GetLength(name)
        letter[i] = Text.GetSubText(name, i, 1)
        value[i] = l[letter[i]]
        if letter[i] <> " " then
          TextWindow.Writeline(letter[i] + "        " + value[i])
        Else
          TextWindow.Write("")
        EndIf 
        tvalue =  tvalue + value[i]
      EndFor
      TextWindow.WriteLine("")
      TextWindow.WriteLine("The total value for your word/name, " +name+ " = " + tvalue)

      it is this bit that stores the info

      Code: [Select]
      l = "a=1;b=2;c=3;d=4;e=5;f=6;g=7;h=8;i=9;j=10;k=11;l=12;m=13;n=14;o=15;p=16;q=17;r=18;s=19;t=20;u=21;v=22;w=23;x=24;y=25;z=26"

      is there a way of doing this in vbs?

      BC_Programmer


        Mastermind
      • Typing is no substitute for thinking.
      • Thanked: 1140
        • Yes
        • Yes
        • BC-Programming.com
      • Certifications: List
      • Computer: Specs
      • Experience: Beginner
      • OS: Windows 11
      Re: VBS Script...need to know something
      « Reply #14 on: February 04, 2010, 03:20:18 AM »
      Code: [Select]
      dim name,value
      Const lowercaseA=97
      name=inputbox("Input Your Name to Get a Value:","Input Name")
      name2=LCase(name)
      length = len(name)

      for i = 1 to length
      charasc=asc(mid(name2,i,1))

      if charasc >= lowercaseA and charasc < lowercaseA+26 then
      value = value + (charasc)-lowercaseA+1
      else
      value = value + charasc
      end if
      next


      msgbox value

      The smaller segment was a portion of the original script.
      I was trying to dereference Null Pointers before it was cool.

      thunder708

        Topic Starter


        Rookie

        Re: VBS Script...need to know something
        « Reply #15 on: February 04, 2010, 03:25:40 AM »
        ah right thank you =D it works fine now. cheers for the help

        BC_Programmer


          Mastermind
        • Typing is no substitute for thinking.
        • Thanked: 1140
          • Yes
          • Yes
          • BC-Programming.com
        • Certifications: List
        • Computer: Specs
        • Experience: Beginner
        • OS: Windows 11
        Re: VBS Script...need to know something
        « Reply #16 on: February 04, 2010, 03:29:24 AM »
        ah right thank you =D it works fine now. cheers for the help

        yes, and thanks for geek-9pm pointing out the missing addition :)
        I was trying to dereference Null Pointers before it was cool.

        Geek-9pm


          Mastermind
        • Geek After Dark
        • Thanked: 1026
          • Gekk9pm bnlog
        • Certifications: List
        • Computer: Specs
        • Experience: Expert
        • OS: Windows 10
        Re: VBS Script...need to know something
        « Reply #17 on: February 04, 2010, 11:10:26 AM »
        So this thread can be closed.
        But  I wanted to ask BC if he has a favorite VBscript Editor and debugger?

        BC_Programmer


          Mastermind
        • Typing is no substitute for thinking.
        • Thanked: 1140
          • Yes
          • Yes
          • BC-Programming.com
        • Certifications: List
        • Computer: Specs
        • Experience: Beginner
        • OS: Windows 11
        Re: VBS Script...need to know something
        « Reply #18 on: February 04, 2010, 01:11:10 PM »
        So this thread can be closed.
        But  I wanted to ask BC if he has a favorite VBscript Editor and debugger?

        I don't work with VBScript much, but I use editpad for editing scripts/text files.
        I was trying to dereference Null Pointers before it was cool.

        Geek-9pm


          Mastermind
        • Geek After Dark
        • Thanked: 1026
          • Gekk9pm bnlog
        • Certifications: List
        • Computer: Specs
        • Experience: Expert
        • OS: Windows 10
        Re: VBS Script...need to know something
        « Reply #19 on: February 04, 2010, 01:58:07 PM »
        Than ks. Looks good. And is FREE for personal use.
        EditPad™ Pro
        http://www.editpadpro.com/

        BC_Programmer


          Mastermind
        • Typing is no substitute for thinking.
        • Thanked: 1140
          • Yes
          • Yes
          • BC-Programming.com
        • Certifications: List
        • Computer: Specs
        • Experience: Beginner
        • OS: Windows 11
        Re: VBS Script...need to know something
        « Reply #20 on: February 04, 2010, 02:40:08 PM »
        Than ks. Looks good. And is FREE for personal use.
        EditPad™ Pro
        http://www.editpadpro.com/

        Editpad Lite is free for personal use. Editpad Pro is not.

        Editpad Lite doesn't have things like syntax highlighting and other useful (for scripting) features like that. The Pro Version isn't free but it's certainly worth it (to me)
        I was trying to dereference Null Pointers before it was cool.