Issue with lists in function arguments

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.

Declares it as a string. Will have a look at that and see if I can duplicate it. Thanks for the reply.

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"

Hope that helps!

A runnable code example actually demonstrating the issue :wink:

You describe some code, and have some code lines without any context, which isn’t quite enough to go on.

This is an example based on the code you gave us:

function test(testString) {
    writeoutput("
        len = #listLen(arguments.testString)#<br>
        getAt = #listGetAt(arguments.testString,2)#
    ")
}

test("one,two")

(TryCF.com)

And it works fine:

len = 2
getAt = two

So. Whatever is going on for you is going on in code you have not shown us.

Hence it being pretty vital to derive a runnable case that demonstrates the issue as a starting point.

Recommended reading: http://www.sscce.org/

2 Likes

Yep, was converting it.

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. :slight_smile: - was a long day…

Thanks all for the replies

This can be closed/deleted.

2 Likes

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 :slight_smile: So I always hope that when someone has the same issue and they end up here, they might also find a similar solution.

2 Likes