You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@groovy.apache.org by James McMahon <js...@gmail.com> on 2023/03/04 22:10:42 UTC

Re: Dynamic assignment of list name in iterator statement?

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 <so...@glasius.dk>
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 <jsmcmahon3@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
>>
>>

Re: Dynamic assignment of list name in iterator statement?

Posted by Rachel Greenham <ra...@merus.eu>.
For the love of god use Locale 😀

  

  

Set<Locale> containedLanguages = new HashSet<>()

  

containedLanguages << Locale.ENGLISH

if (containedLanguages.contains(Locale.ENGLISH)) …

  

Also you can search the available locales

  

foundLocale = Locales.availableLocales.find { it.displayName == languageName }

  

Etc etc etc.

  

\--

Rachel

  

> On 5 Mar 2023, at 08:37, James McMahon <js...@gmail.com> wrote:  
>  
>

> 
>
> 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
> <[jsmcmahon3@gmail.com](mailto:jsmcmahon3@gmail.com)> 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
<[soeren@glasius.dk](mailto:soeren@glasius.dk)> 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](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
<[jsmcmahon3@gmail.com](mailto:jsmcmahon3@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

>>>>

>>>>  
>


Re: Dynamic assignment of list name in iterator statement?

Posted by James McMahon <js...@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 <so...@glasius.dk> 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 <jsmcmahon3@gmail.com
> >:
>
>> 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 <js...@gmail.com>
>> 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 <so...@glasius.dk>
>>> 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 <
>>>> jsmcmahon3@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
>>>>>
>>>>>

RE: Dynamic assignment of list name in iterator statement?

Posted by Bob Brown <bo...@transentia.com.au>.
Forgive me if I am misunderstanding, but does one of the following options do what you need?

===
println "Option 1"
def langs = ['english', 'spanish']

def englishMethod() { 'hello, old chap!' }
def spanishMethod() { 'hola! Como esta?' }

langs.each { lang ->
  println(lang + ': ' + "${lang}Method"())
}

println "Option 2"
def fns = [
('english'): { -> 'hello, old chap!' },
('spanish'): { -> 'hola! Como esta?' }
]

langs.each { lang ->
  println(lang + ': ' + fns[(lang)]())
}
===

Runs in groovyConsole/4.0.9/jdk20 like:

“””
groovy> println "Option 1"
groovy> def langs = ['english', 'spanish']
groovy> def englishMethod() { 'hello, old chap!' }
groovy> def spanishMethod() { 'hola! Como esta?' }
groovy> langs.each { lang ->
groovy>   println(lang + ': ' + "${lang}Method"())
groovy> }
groovy> println "Option 2"
groovy> def fns = [
groovy> ('english'): { -> 'hello, old chap!' },
groovy> ('spanish'): { -> 'hola! Como esta?' }
groovy> ]
groovy> langs.each { lang ->
groovy>   println(lang + ': ' + fns[(lang)]())
groovy> }

Option 1
english: hello, old chap!
spanish: hola! Como esta?
Option 2
english: hello, old chap!
spanish: hola! Como esta?
Result: [english, spanish]
“””

Note: There is some trickiness associated with MUTABLE “GStrings” as keys. It is often best to use string-valued expressions (eg (lang), as shown).
See: http://docs.groovy-lang.org/latest/html/documentation/index.html#_gstring_and_string_hashcodes

Note also, it is perfectly OK to have methods named like:

===
def  “A very…long…{weird} name!”() { }

def "null"() { true }  // not recommended; potentially confusing
===

The Spock and Geb frameworks use this feature to great effect…

HTH

BOB

From: Søren Berg Glasius <so...@glasius.dk>
Sent: Sunday, March 5, 2023 9:32 PM
To: users@groovy.apache.org
Subject: Re: Dynamic assignment of list name in iterator statement?

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 <js...@gmail.com>>:
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 <js...@gmail.com>> 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 <so...@glasius.dk>> 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 <js...@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


Re: Dynamic assignment of list name in iterator statement?

Posted by Søren Berg Glasius <so...@glasius.dk>.
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 <js...@gmail.com>:

> 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 <js...@gmail.com> 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 <so...@glasius.dk>
>> 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 <
>>> jsmcmahon3@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
>>>>
>>>>

Re: Dynamic assignment of list name in iterator statement?

Posted by James McMahon <js...@gmail.com>.
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 <js...@gmail.com> 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 <so...@glasius.dk>
> 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 <
>> jsmcmahon3@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
>>>
>>>