Re: Dynamic assignment of list name in iterator statement?

James McMahon <[email protected]>
Newsgroups gmane.comp.lang.groovy.user
Message-ID <CAEqJCe0WKM08Ag681jcycUN80mmsO__HojyT5ibEbtfoqSonCw@mail.gmail.com>
Yes indeed. A careless error one makes in the early hours of the morning.
Thank you very much  Søren.
I am also going to look into Locale as Rachel G. encouraged 😉. And Bob B.
has replied with a sample I want to explore too. Thank you Bob and thank
you Rachel.
Jim

On Sun, Mar 5, 2023 at 6:32 AM Søren Berg Glasius <[email protected]> wrote:

> Hi Jim,
>
> If your switch hits "English" it will also set the rest of the cases. You
> need a "break" after "containsEnglish = true" - just like in Java
>
>
> Med venlig hilsen,
> 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 søn. 5. mar. 2023 kl. 09.37 skrev James McMahon <[email protected]
> >:
>
>> Was trying to come up with a Groovy way to collapse a lengthy switch
>> statement to dynamically building the variable name. I've failed at that.
>> Instead, I've fallen back on this option:
>>
>>      switch("$k") {
>>                case "English":
>>                     containsEnglish = true
>>                case "Spanish":
>>                     containsSpanish = true
>>                case "French":
>>                     containsFrench = true
>>                case "Japanese":
>>                     containsJapanese = true
>>                case "German":
>>                     containsGerman = true
>>                .
>>                .
>>                .
>>                default:
>>                     break
>>           }
>>
>> I initialize each of my "containsXYZ" variables to false at the beginning
>> of my Groovy script. It works well, though it seems to lack elegance and
>> brevity to me.
>>
>> Thanks again.
>> Jim
>>
>> On Sat, Mar 4, 2023 at 5:10 PM James McMahon <[email protected]>
>> wrote:
>>
>>> 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.