'Invalid expression' when copying and pasting a functional Arcade expression between label classes, only changing one string

837
5
08-08-2022 01:19 PM
Labels (1)
wayfaringrob
Frequent Contributor

I'm scratching my head at this; I have an Arcade expression used to drive interstate labels that I'm trying to copy over to a label class for US highway labels. In the DOT's STATE_ROUTE fields, these are notated as 'I' and 'US' - for example, 'STATE OF IOWA, I 74 W' or 'STATE OF IOWA, US 6 W'. My expression finds those strings within the field and then returns the appropriate numbered route, if any. However, when I swap the I for US, I get an invalid expression error - wrong number of arguments. I'm not changing the number of arguments, though, and it works fine in the interstate label class. I feel like I'm missing something obvious, but I can't figure it out. I'm just copying the expression over.

Here is the (functional) expression used for interstates:

 

 

IIF(Find('STATE OF IOWA, I',$feature.STATE_ROUT)>='0',Split($feature.STATE_ROUT, ' ', 5, true)[4],IIF(Find('STATE OF IOWA, I',$feature.STATE_RO_1)>='0',Split($feature.STATE_RO_1, ' ', 5, true)[4]))

 

 

It successfully returns the digits of an interstate, such as '80' or '380'. If I swap the 'STATE OF IOWA, I' portions for 'STATE OF IOWA, US', it kicks me the error message. Same exact string otherwise.

rburkebsrc_1-1659989849204.png

 

Tags (1)
0 Kudos
5 Replies
wayfaringrob
Frequent Contributor

I tried removing the label class and trying again with a new label class (did not work). I then tried closing and reopening the project, and now I'm even getting the error when copying it verbatim. Still comes up as valid in the original interstate label class.

rburkebsrc_0-1659990510308.png

rburkebsrc_1-1659990567733.png

 

0 Kudos
wayfaringrob
Frequent Contributor

The 'wrong number of arguments' turned out to be referring to the final IIF (my bad, but not that it tells you that...), but I'm really curious as to why it would have worked just fine for my interstate labels and not US highways. It seems it shouldn't have worked for interstates, either.

rburkebsrc_0-1659991125040.png

0 Kudos
JohannesLindner
MVP Frequent Contributor

Glad you found the error.

Some IDEs only tell you the line where something went wrong, not the exact position. So while it can be satisfying to write one-liners (I'm very guilty of that in Python) they make it hard to find the problem (plus, they are hard to read, and line-wrap doesn't make it better...).

 

if(Find('STATE OF IOWA, I', $feature.STATE_ROUT) > -1) {
    return Split($feature.STATE_ROUT, ' ', 5, true)[4]
}
if(Find('STATE OF IOWA, I', $feature.STATE_RO_1)> -1) {
    return Split($feature.STATE_RO_1, ' ', 5, true)[4]
}
return 'DefaultValue'

 

 

You're right, the expression shouldn't work for interstates, either. I have not the faintest clue why it would have worked, but:

  • Did it actually work (did you see the correct labels)?
  • Does it still work?
  • Have you revalidated the expression (pressed on the checkmark button)?

Have a great day!
Johannes
0 Kudos
wayfaringrob
Frequent Contributor

Yes, it kept revalidating and was displaying my labels perfectly (see screenshot of validated expression above). I've corrected it now just in case, and now won't revalidate if I put it back. Strange!

Also, yes - I typically use variables, maybe more than necessary. They're nice, and certainly would have helped here.

0 Kudos
wayfaringrob
Frequent Contributor

This is a much happier (and easier to modify/reuse) solution.

//route field
var p = $feature.STATE_ROUT

//route type notation
var n = 'STATE OF IOWA, I '

//searches route field for type match, returns number only
IIF(Find(n,p)>='0',Split(p, ' ', 5, true)[4],'')
0 Kudos