Simple way to reproduce the behavior of '&' in a VBScript label expression, using Arcade label expression instead?

159
8
Jump to solution
05-30-2020 01:57 PM
Highlighted
New Contributor II

My question is at the end of the post, but first I will give an example of the behavior I'm trying to replicate... When writing a label expression in VBScript, I sometimes take advantage of the difference between '+' and '&' when concatenating attributes and text. For example, lets say I'm labeling sewer pipes. I could write the following expression:

[LENGTH] & " ft. " & [DIAMETER] & "-inch " & [MATERIAL] & " @ " & [SLOPE] & "%"

 

And assuming the attributes are fully populated, the label might look like this:

50 ft. 12-inch PVC @ 2.5% 

But sometimes we don't have all the information. If an attribute is missing (let's say the slope), the bits of text supporting it, "@" and "%", would still be hanging there:

50 ft. 12-inch PVC @ %

We likely don't want that to happen. By tying two or more elements together with '+' instead of '&', any element tied to a null value will be ignored. Consider the following modified example:

[LENGTH] + " ft. " & [DIAMETER] + "-inch " & [MATERIAL] & " @ " + [SLOPE] + "%"

Now if the slope attribute was null, the "@" and "%" would be left out like this:

50 ft. 12-inch PVC

If just the length was null:

12-inch PVC @ 2.5%

If the diameter and material were null:

50 ft. @ 2.5%

You see we have a fairly flexible expression. Finally, if you were to use only '+' operators, and no '&'...

[LENGTH] + " ft. " + [DIAMETER] + "-inch " + [MATERIAL] + " @ " + [SLOPE] + "%"

...Then whenever any attribute is missing, the entire label will be left out. The effect can be a useful visual cue. A pipe's label would be missing entirely until every attribute needed for the label had been populated.

 

Finally, my question...

In Arcade label expressions, it seems that '+' behaves the same way '&' does in VBScript. Is there a similar easy way to achieve the behavior I've described of the VBScript '+' using Arcade, without to having check for nulls using IsEmpty?

Thanks!!

Reply
0 Kudos
1 Solution

Accepted Solutions
Highlighted
MVP Esteemed Contributor

VBScript's time has come and gone, assuming it ever came in the first place, but I loathe Arcade.  Even if you don't take issue with Esri creating a new, proprietary language, which I do take issue with, usability wasn't a design priority if JavaScript was the inspiration for syntax and structure.

The short answer to your question, no, there is no equivalent behavior in Arcade for what you are doing in VBScript.  That said, what you are doing with VBScript isn't even a feature of VBScript, it is an Esri VBScript implementation behavior that won't transfer to other applications.

Something along the lines of below should work for you and not be too convoluted in terms of structure:

var flds = [$feature.LENGTH, $feature.DIAMETER, $feature.MATERIAL, $feature.SLOPE]
var pref = ["", " ", " ", " @ "]
var suff = [" ft.", "-inch", "", "%"]

var lbl = ""
for(var k=0; k<Count(flds); k++) {
if (IsEmpty(flds[k])) continue;
lbl = lbl + pref[k] + flds[k] + suff[k];
}
return lbl;

View solution in original post

8 Replies
Highlighted
MVP Regular Contributor

I think I've done it ok in python, I'm sure there's a better way though. I'm not sure how the locals() will go in arcade as not very familiar with the syntax, is it just 'var' infront of every variable?

length = 2.5
diameter = 1
#material =" WOOD"
slope = 25

if 'length' in locals():
length = str(length) + " ft. "
else:
length = ""
if 'diameter' in locals():
diameter = str(diameter) + "-inch"
else:
diameter = ""
if not 'material' in locals():
material = ""
if 'slope' in locals():
slope = " @" + str(slope) + "%"
else:
slope = ""

return (length + diameter + material + slope)

Reply
0 Kudos
Highlighted
MVP Regular Contributor

actually 

length = "LENGTH! etc. so can just be if length: instead of if 'length' in locals(): same for the rest.

Reply
0 Kudos
Highlighted
MVP Esteemed Contributor

VBScript's time has come and gone, assuming it ever came in the first place, but I loathe Arcade.  Even if you don't take issue with Esri creating a new, proprietary language, which I do take issue with, usability wasn't a design priority if JavaScript was the inspiration for syntax and structure.

The short answer to your question, no, there is no equivalent behavior in Arcade for what you are doing in VBScript.  That said, what you are doing with VBScript isn't even a feature of VBScript, it is an Esri VBScript implementation behavior that won't transfer to other applications.

Something along the lines of below should work for you and not be too convoluted in terms of structure:

var flds = [$feature.LENGTH, $feature.DIAMETER, $feature.MATERIAL, $feature.SLOPE]
var pref = ["", " ", " ", " @ "]
var suff = [" ft.", "-inch", "", "%"]

var lbl = ""
for(var k=0; k<Count(flds); k++) {
if (IsEmpty(flds[k])) continue;
lbl = lbl + pref[k] + flds[k] + suff[k];
}
return lbl;

View solution in original post

Highlighted
MVP Esteemed Contributor

I loathe Arcade.  Even if you don't take issue with Esri creating a new, proprietary language, which I do take issue with, usability wasn't a design priority if JavaScript was the inspiration for syntax and structure.

I could not agree more. "That @#$% circus language"

Highlighted
Esteemed Contributor

Thanks for the example, the best part of GeoNet.

Reply
0 Kudos
Highlighted
New Contributor II

Thanks for your reply and example Joshua! I knew (I think mainly from pro training vids) that VBScript is out and Arcade is in, but I'm too naive to feel ways about it, haha. Since you mention it, I guess it does kinda suck that they went and created a something proprietary. Now I'm wondering how worthwhile it is to work on updating my label expressions in Arcade in the first place. Perhaps I should try to use Python more instead.

Reply
0 Kudos
Highlighted
MVP Esteemed Contributor

No fan of arcade either, but won't concatenate() do the same thing as '&' in vb?

Reply
0 Kudos
Highlighted
New Contributor II

Forgive me Joe. The title of my post suggests I'm looking to do what '&' does in vb, but my question is actually asking about replicating the '+' behavior from vb, in contrast to '&'.

Reply
0 Kudos