Beginner Arcade question - Split function - extracting values

2342
4
Jump to solution
08-08-2022 04:32 PM
Labels (1)
wayfaringrob
Frequent Contributor

I thought I knew how to get a value out of an array, but I'm having trouble. I'm looking to return the 2nd, or middle value - the string between two commas in my field:

rburkebsrc_1-1660001093221.png

rburkebsrc_0-1660000996311.png

The split function itself works...

rburkebsrc_2-1660001116505.png

rburkebsrc_3-1660001123371.png

And I can ask for the first value successfully...

rburkebsrc_6-1660001385902.png

rburkebsrc_4-1660001372238.png

But when I ask for that middle value, it says it's out of bounds. I feel that I'm missing something obvious and would appreciate any pointers!

0 Kudos
2 Solutions

Accepted Solutions
jcarlson
MVP Esteemed Contributor

Seems to work fine, but if you have any values in your table that don't follow the same formatting, it's possible that you could see an out of bounds message.

Some pointers:

Unless you're positive that the data's all good and it's never going to change, assume there will be some "breaking" values or nulls in your fields and try to account for them. The DefaultValue function is good for this in other contexts, but won't catch out-of-bounders.

You can also use Console to send messages to the console window. For longer expressions, this is good for checking intermediate outputs, or to see how far an expression gets before failing, if your error message isn't specific or helpful enough.

Utilize variables. I get wanting to do a nice one-liner, but if you're having trouble getting your expression to work, breaking it into pieces can make for an easier debugging process.

All that said, what does your not-working expression look like? I don't see how changing the index from 0 to 1 would break this...

var a = 'CITY OF BETTENDORF, FOREST GROVE DRIVE, E'
var s = Split(a, ',')
var c = Count(s)

Console(`${c} items in split array`)

If (c > 1){
    return s[1]
} else {
    return 'Only one item in the split array.'
}

jcarlson_4-1660008610013.pngjcarlson_5-1660008621292.png

jcarlson_6-1660008705659.png

 

 

 

- Josh Carlson
Kendall County GIS

View solution in original post

0 Kudos
wayfaringrob
Frequent Contributor

Oh, wait a minute. Using the COUNT function works ... perfectly. Did not know about that. Look at me making things as complex as can be. The solution is pretty simple. Thanks.

rburkebsrc_3-1660057731887.png

 

View solution in original post

0 Kudos
4 Replies
DanPatterson
MVP Esteemed Contributor

The number returns the number of splits to make, the default being -1 which means all.  Then you slice.

If you know that there will be 3, then a slice of [1] will return the middle

Text Functions | ArcGIS Arcade | ArcGIS Developers

You can do it in python and probably emulate it in  arcade

fld0 = "a, b"
fld1 = "a, b, c"
fld2 = "a, b, c, d"
fld3 = "a, b, c, d, e"

fld0.split(", ")[fld0.count(",")//2]
'a'

fld1.split(", ")[fld1.count(",")//2]
'b'

fld2.split(", ")[fld2.count(",")//2]
'b'

fld3.split(", ")[fld3.count(",")//2]
'c'

... sort of retired...
0 Kudos
jcarlson
MVP Esteemed Contributor

Seems to work fine, but if you have any values in your table that don't follow the same formatting, it's possible that you could see an out of bounds message.

Some pointers:

Unless you're positive that the data's all good and it's never going to change, assume there will be some "breaking" values or nulls in your fields and try to account for them. The DefaultValue function is good for this in other contexts, but won't catch out-of-bounders.

You can also use Console to send messages to the console window. For longer expressions, this is good for checking intermediate outputs, or to see how far an expression gets before failing, if your error message isn't specific or helpful enough.

Utilize variables. I get wanting to do a nice one-liner, but if you're having trouble getting your expression to work, breaking it into pieces can make for an easier debugging process.

All that said, what does your not-working expression look like? I don't see how changing the index from 0 to 1 would break this...

var a = 'CITY OF BETTENDORF, FOREST GROVE DRIVE, E'
var s = Split(a, ',')
var c = Count(s)

Console(`${c} items in split array`)

If (c > 1){
    return s[1]
} else {
    return 'Only one item in the split array.'
}

jcarlson_4-1660008610013.pngjcarlson_5-1660008621292.png

jcarlson_6-1660008705659.png

 

 

 

- Josh Carlson
Kendall County GIS
0 Kudos
wayfaringrob
Frequent Contributor

Thanks for taking the time to show examples! And yes, I do normally use variables - they're great!

I also don't see how changing the index from [0] to [1] would break it, but alas, that's the issue.

Valid:

var muni = $feature.MUNICIPAL_
var label = split(muni,",",3,false)
return string[0]

Invalid (index out of bounds):

var muni = $feature.MUNICIPAL_
var label = split(muni,",",3,false)
return string[1]

When I test with text, I have no problems.

rburkebsrc_2-1660056176439.png

Some fields do not follow this format, but my attempts at excluding those from the expression haven't been successful.

Valid expression, with [0]:

var muni = $feature.MUNICIPAL_
var comma1_position = Find(",",muni)
var comma2_start = comma1_position + 1

//returns '' if 2 comma delimiters are not found; returns first string if 2 commas are found
IIF(comma1_position == "-1","",IIF(Find(",",muni,(comma2_start)) == "-1","", split(muni,",",3,false)[0]))

 Invalid expression, with [1] (nothing else changed) (index out of bounds error):

var muni = $feature.MUNICIPAL_
var comma1_position = Find(",",muni)
var comma2_start = comma1_position + 1

//returns '' if 2 comma delimiters are not found; returns second string if 2 commas are found
IIF(comma1_position == "-1","",IIF(Find(",",muni,(comma2_start)) == "-1","", split(muni,",",3,false)[1]))

 

0 Kudos
wayfaringrob
Frequent Contributor

Oh, wait a minute. Using the COUNT function works ... perfectly. Did not know about that. Look at me making things as complex as can be. The solution is pretty simple. Thanks.

rburkebsrc_3-1660057731887.png

 

0 Kudos