ColdFusion ListGetAt() vs. GetToken()

The other day I was asked which was better to use, ColdFusion's ListGetAt() or GetToken(). I think in 8 years of ColdFusion development I have used GetToken() only once or twice? Off hand, I didn't even know what it did exactly or specifically, what it did that was so different. From a cursory glance at the documentation, the two appears to be the same thing: they both take a list, an index, and a set of delimiters. It's not till I really looked at the ColdFusion documentation that I really saw that they are performing two similar but different functions.

Both functions do indeed get specific elements of a list, but they are different in two very important respects:

LetGetAt()'s default delimiter is the comma. GetToken()'s default delimiter is actually a set of delimiters including the majority of white space characters (space, tab, and new line).

If you reference an index that is beyond the end of the list, ListGetAt() will throw an exception where as GetToken() will just return an empty string.

To see this in action, I have set up a little demo in which we loop over the items in a list, going beyond the boundaries on purpose. For the first demo, we are going to ask both methods to use the comma as the list delimiter:

Notice that when we go lower than the list length (index zero), both ColdFusion functions throw an error. While we are in the meat of the list, both function in exactly the same (with the explicit comma delimiter). Once we go past the end of the list, ListGetAt() throws an exception while GetToken() just returns an empty string.

Ok, but that's with both methods using explicit delimiters. Let's take a look at how GetToken() works if you let it use its default delimiter set. This time, our list will have line breaks and other white space rather than commas. For the ListGetAt(), we are going to build up a set of delimiters to use so that it finds more than just the first "element":

<!---

Create a new list. This time, though, we are not going

to be using the comma as a delimiter; we are basically

going to be using whitespace as the delimiter.

--->

<cfsavecontent variable="strList">

Frances Mcdormand sort

of has it going on?!?

</cfsavecontent>

<!--- Set up the string of delimiters (white space). --->

<cfset strDelimiters = (

" " &

Chr( 13 ) &

Chr( 10 ) &

Chr( 9 )

) />

<!---

To test how each method works, not only are we going

to loop over the elements in the list, we are going

to do so in an out-of-bounds fashion; notice that we

are starting before 1 and going until after the end

of the list.

--->

<cfloop

index="intI"

from="0"

to="#(ListLen( strList, strDelimiters ) + 1)#"

step="1">

<!---

Because these methods might error, we are going

to wrap each one its own CFTry / CFCatch block

and display any caught errors.

--->

<p>

ListGetAt( #intI# ):

<cftry>

#ListGetAt( strList, intI, strDelimiters )#

<!--- Catch error and display message. --->

<cfcatch>

ERROR: #CFCATCH.Message#

</cfcatch>

</cftry>

<br />

GetToken( #intI# ):

<!---

This time, notice that we are not passing

in any delimiters to the GetToken() method.

This will force GetToken() to use its default

set of delimiter characters.

--->

<cftry>

#GetToken( strList, intI )#

<!--- Catch error and display message. --->

<cfcatch>

ERROR: #CFCATCH.Message#

</cfcatch>

</cftry>

</p>

</cfloop>

Since ListGetAt() uses the comma by default in lists, we need to tell it to use all those white space characters as its list delimiters. Running the above code, we get the following output:

Going outside the list boundaries works just like it did in the first demo; the real difference here is that GetToken(), without being given any explicit delimiters, was able parse the white-space-delimited list.

Ok, so nothing really revolutionary here, more just personal exploration; see, this is what happens when you don't read documentation - you don't even know what functions are supposed to do. At least now I know why these two functions coexist. I am not going to bother doing any speed tests on the two different methods because I don't use non-comma-delimited lists all that often, and so, I am not often in a position where GetToken() adds a functional benefit. I assume the two work in a similar fashion.

Also realize, however, that ListGetAt() can also use multiple delimiters at the same time (which is what I am doing in the second example). But, explicit delimiters aside, GetToken() will not error if you go past the list, ListGetAt() will.

I think it comes down to a matter of convenience; if you are parsing something that uses white space as a delimiter, you should probably use GetToken() rather than using ListGetAt() with explicit delimiters.

ListGetAt can use multiple delimiters, but each of them can be no more than one character in length. E.g., with ListGetAt, the delimiter value "test" is four discrete delimiters ("t", "e", "s", "t"), while with GetToken it is a single, four-character delimiter. IMO, that is a much more significant difference than the functions' default delimiters or whether they error outside of bounds.

Ben, I'm not sure. In fact, I only learned about the GetToken function from your post here, but it struck me as significant because I've often wanted to use multi-character delimiters with the CF list functions.

Yeah, the documentation is very confusing. I don't understand that whole default list delimiter as comma in the context that it would work as you say it would. I think the documentation must be incorrect... either that or I am just totally missing something here.

Lovely. This is why ColdFusion really bugs me sometimes. If you look at the documentation, they have not fixed it online, causing it to populate into CF 7 and 8 docs, even though as of CF 6 it's been known to be incorrect. Having wasted half my morning working on this (because this is *exactly* the functionality I need and it sure seemed possible), I'm rather frustrated at them. :-/ Particularly because it's not the only time their docs have been completely off the mark or very confusing.

Sigh, didn't mean to vent. Looks like I'll be developing my own multi-char delimiter parsing function. :-) I don't mind doing it....I just wish Adobe hadn't led me on a wild-goose-chase with their invalid docs.

I am the co-founder and lead engineer at InVision App, Inc — the world's leading prototyping,
collaboration & workflow platform. I also rock out in JavaScript and ColdFusion 24x7 and I dream about
promise resolving asynchronously.