Label expression

2502
6
03-18-2016 04:01 AM
TomasLooström
New Contributor II

Hi,

I need help with Label Expression.

I am using a VBScript to get the name of a building (it can have double name like NÄS GANS) from TRAKT and the the number of the building (1:12 or something like that). I do not want to print out the first part of the name, in the case above GANS. The code works fine, except when a building has not got a double name (NÄS), then nothing is printing.

Function FindLabel ( [TRAKT], [BLOCKENHET] )

  FindLabel = "<CLR red = '255'>" &Split(  [TRAKT], " ", 2)(1) & "</CLR>" +" "+ [BLOCKENHET]

End Function

I have attached an image to make myself understandably:)

Please could anyone help?

Best regards,

Tomas Looström

Tags (2)
0 Kudos
6 Replies
DanPatterson_Retired
MVP Emeritus

I see the logic... I think... but I use python which uses zero-based indexing and allows negative slicing (ie slicing from the end of a sequence)

So if this is correct, you do it in the language of choice

>>> a = "Nas s:101>2"
>>> b = "Nas Gans 1:12>5"
>>> 
>>> print(a.split(" ")[0] + "\n" + a.split(" ")[-1])
Nas
s:101>2
>>> print(b.split(" ")[0] + "\n" + b.split(" ")[-1])
Nas
1:12>5
>>>
JimmyKroon
Occasional Contributor II

You could add an if/else statement to check if the building has two names. (Fair warning, I'm also more familiar with Python so my VB syntax probably isn't correct.)

dim buildingName as string

if Split(  [TRAKT], " ", 2) = "" then buildingName = [TRAKT]    ## If 2nd word in TRAKT is empty, then use the entire cell value.

else buildingName = Split(  [TRAKT], " ", 2)

Then build your label expression using buildingName.

0 Kudos
TomasLooström
New Contributor II

Hi,

I am no programmer:/ So if any of You know how to write it in Python I am more than happy:)

0 Kudos
DanPatterson_Retired
MVP Emeritus

I did...sort of... just replace my variable names with your field names... I don't do labelling so I don't know if it uses python field calculator syntax with ! stuff ! enclosing exclamation marks or [ ] like old VB stuff

0 Kudos
JimmyKroon
Occasional Contributor II

I think this will work.

def FindLabel ( [TRAKT] 😞

  if " " not in [TRAKT]:

    Building = [TRAKT]

  else:

    Building = [TRAKT].split(" ")[1]

  return "<CLR red = '255'>" + Building + "</CLR>" + " " + [BLOCKENHET]

0 Kudos
TedKowal
Occasional Contributor III

... one way to do it in VBscript  (Original question implied vbScript)

dim NumberOfWords, myLabel
dim YourField


YourField = [YourStringField]


ArrayTemp = split(YourField," ")
NumberOfWords = UBound(ArrayTemp) + 1
Select Case NumberOfWords
  case 1  'One word
      myLabel = YourField
  case 2   'Two Words
      myLabel = YourField
  case 3   'Three words as in your example
      myLabel = ArrayTemp(1) & "" & ArrayTemp(2)   'Second and Last Word
  case else  'more than 3 words
      myLabel = YourField 'or further processing
end select
0 Kudos