Re: Dynamic assignment of list name in iterator statement?
Rachel Greenham <[email protected]>
| Newsgroups | gmane.comp.lang.groovy.user |
|---|---|
| Message-ID | <[email protected]> |
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 <[email protected]> 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 <[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