Re: Dynamic assignment of list name in iterator statement?

James McMahon <[email protected]>
Newsgroups gmane.comp.lang.groovy.user
Message-ID <CAEqJCe3-qB7ykhMLGWSVaYSqwNmgbjqXvVanxCJQ-aU8qfuxRA@mail.gmail.com>
Søren  ,
May I ask you a follow up? I am trying what I thought I read in your reply
(thank you for that, by the way). But I continue to get this error:
"The LHS of an assignment should be a variable or a field accessing
expression @ ...."

This is what I currently have, attempting to set my variable name to
include the key drawn from my Groovy map. How must I change this to get it
to work?

     mapLanguages.each { k, x ->
          log.warn('mapLanguages entry is this: {} {}', ["$k", "$x"] as
Object[])
          x.each {
               languageChar -> log.warn('language char in {} is this: {}',
["$k", "$languageChar"] as Object[])
          }
          "contains${k}" = true
     }

Many thanks again,
Jim

On Thu, Feb 23, 2023 at 3:01 AM Søren Berg Glasius <[email protected]>
wrote:

> 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 <[email protected]
> >:
>
>> 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
>>
>>
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.