I’m passing a comma separated list as a string into a function, e.g. “testString” set to “element1,element2”
Inside the function listLen(arguments.testString) correctly gives me 2 but listGetAt(arguments.testString,2) gives me an error of “invalid string list index [2]”
If I create an explicit list in the function then listGetAt works as expected, issue is when pointing it at the arguments variables.
I tried converting it to an array using listToArray and then len(testStringArray) gives me 2 elements but testStringArray[2] gives me an error the same.
Tried evaluating it and assigning it directly to another variables, still errors.
What am I missing here?
Windows 10
11.0.11 (AdoptOpenJDK) 64bit
Apache Tomcat/9.0.46
Lucee 5.3.8.201
Does your function declaration indicate a type for that incoming argument? Ifnso, what type?
Also you might help us help you if you out together a little few-line demo of that function and your call to it. You can even put it on trycf.com, then use ctrl-a there which will give you a url to that, which you can share here and we can run it.
As you said, you are passing a string, you cant access items in a string using array notation as far as I can tell, as it’s a string, not a collection of any kind. For example:
var myString = "Elvis Is The Best, Person, In the world"
var thing = myString[2];
What should myString[2] return? The second character? The second item in a list? The second word?
That’s why the notation doesnt work with strings. So you can do this:
var myString = "Elvis Is The Best, Person, In the world"
var myCommaArray = ListToArray(myString, ",");
echo(myCommaArray[2]); // "Person"
var mySpaceArray = ListToArray(myString, " ");
echo(mySpaceArray[2]); // "Is"
My bad, went through it all again this morning. The function was being called a second time, that I’d missed, and I was getting the error from that call rather than my testing. - was a long day…
Glad it worked out! Sometimes this forum can help, if even finding a different error.
Also, this is just a forum of people trying to help rather than an “issue” management system So I always hope that when someone has the same issue and they end up here, they might also find a similar solution.