-1

I have two Python script snippets that need to convert to VBscript.

Can anybody help me because I am really not familiar and know Python.

the first script is

def FindLabel([BA2]):
    name = str([BA2])
    if "-" in name:
        first, second = name.split("-",1)
        for letter in first:
            if not letter.isalnum():
                first = first.strip(letter)
        return first
    else:
    return name

the second script is:

def FindLabel([BA2]):
    name = str([BA2])
    if "-" in name:
        first, second = name.split("-",1)
        for letter in second:
            if not letter.isalnum():
                second = second.strip(letter)
        return second   
    else:
        return name
4
  • 1
    Why do you want to convert them? Since 10.1 there has been a Python parser for Label Expressions and you appear to be using 10.2.
    – PolyGeo
    May 28, 2014 at 7:48
  • Note that you should avoid using [] is the name of a variable.
    – radouxju
    May 28, 2014 at 13:09
  • the reason why i want to use the vb script, i want to standardize it since other expression i used vbscript..
    – user
    May 29, 2014 at 1:44
  • Standardizing on Python would make more sense to me.
    – PolyGeo
    May 29, 2014 at 4:33

1 Answer 1

1

It's not easy, the functions in VBScript are limited. I think you would be better standardizing to python as it offers far more functions and options than VBScript which is on the way out. However, in the interest of just answering the question...

As I understand it you want to take the first word if there is a dash but only display the alphanumeric characters and if there is no dash then just the contents of BA2.

Function FindLabel ([BA2])
     if InStr([BA2],"-") > 0 then
       BA_Split = split([BA2],"-")
       ThisWord = BA_Split(0)
       OutString = ""
       for index = 1 to len(ThisWord) 
         SingleLetter = mid(ThisWord,index,1)
         if asc(SingleLetter) >= 48 and asc(SingleLetter) <= 57 then
           OutString = OutString & SingleLetter
         elseif asc(SingleLetter) >= 65 and asc(SingleLetter) <= 90 then
           OutString = OutString & SingleLetter
         elseif asc(SingleLetter) >= 97 and asc(SingleLetter) <= 122 then
           OutString = OutString & SingleLetter
         end if
       next
       FindLabel = OutString
     else
       FindLabel = [BA2]
     end if
End Function

There is no isalnum() in VBScript, so instead I use the ASCII ranges for 0 to 9, A to Z and a to z... if in this range then append to OutString

The second function is the same as the first except you use the second word:

Function FindLabel ([BA2])
     if InStr([BA2],"-") > 0 then
       BA_Split = split([BA2],"-")
       ThisWord = BA_Split(1)
       OutString = ""
       for index = 1 to len(ThisWord) 
         SingleLetter = mid(ThisWord,index,1)
         if asc(SingleLetter) >= 48 and asc(SingleLetter) <= 57 then
           OutString = OutString & SingleLetter
         elseif asc(SingleLetter) >= 65 and asc(SingleLetter) <= 90 then
           OutString = OutString & SingleLetter
         elseif asc(SingleLetter) >= 97 and asc(SingleLetter) <= 122 then
           OutString = OutString & SingleLetter
         end if
       next
       FindLabel = OutString
     else
       FindLabel = [BA2]
     end if
End Function
2
  • yup,,phyton far more function but im really not familiar with that..thank you so much for helping me..its really help me to solve my problem..thank you once again..
    – user
    May 29, 2014 at 4:33
  • You're welcome. I recommend looking into python, it reads almost as well as VBSCript but can do much more. I don't like the editor interface with the label scripts so I write in another editor and copy in. May 29, 2014 at 4:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.