Hi Jim,

It is possible:

languages = ['english', 'french', 'spanish']
englishCharsList = ['a','b']
frenchCharsList = ['c','d']
spanishCharsList = ['e','f']

languages.each { lang ->
    this."${lang}CharsList".each { ch ->
        println "$lang -> $ch"
    }
}

Check it out here:
https://gwc-experiment.appspot.com/?g=groovy_3_0&codez=eJxVjkEKwyAQRfeeYhDBTZobtJtue4PShbVGBRmCY1fBu2e0ppBZDMN__38mGfRf4x3BFZ7aoU-Rgp5AL9mh7RetBpv4EgPfg8n0iFR6xuhJvxn-AmdmmX2YjYozdAwXhiIdP8zO2AAbNAEuNwE8JUSapdqaVv8F8rDyGsY2a45YEoJUowKUDbLjKqrYAZXRSNo


Best regards,
Søren Berg Glasius

Hedevej 1, Gl. Rye, 8680 Ry
Mobile: +45 40 44 91 88
--- Press ESC once to quit - twice to save the changes.


Den tor. 23. feb. 2023 kl. 01.52 skrev James McMahon <jsmcmah...@gmail.com>:

> Good evening. I have a list named languageCharactersList. I begin my
> iteration through elements in that list with this:
>
> languageCharactersList.eachWithIndex( it, i ->
>
> I hope to make this more generic, so that I can build a variable name that
> points to the appropriate list, which then allows me to keep my iteration
> loop generic.
>
> I'd like to do this:
> def languages = ['english', 'french', 'spanish']
> def englishCharsList = [....]
> def frenchCharsList = [.....]
> def spanishCharsList = [....]
>
> I'll set up an iterator to grab each of the languages. Within that
> iterative loop I will set a general variable like so:
> def CharsList = "english"+"CharsList" (then "french", then "spanish",.....)
>
> I was hoping I could then set up the generic iterator like so:
> *"$CharsList"*.eachWithIndex{ it, i ->
> or like so
> *$CharsList*.eachWithIndex{ it, i ->
>
> But Groovy doesn't allow this approach, and throws a stack trace.
>
> How can we employ a variable assignment in that list iterator statement
> so it can be generalized?
>
> Thanks in advance.
> Jim
>
>

Reply via email to