Groovy convert list of maps into a map - groovy

Hi I know this question has a thread already here but when I applied the answer provided on the thread it didn't work for me. So requesting for help with my exact problem below:
So I am trying to convert: [[xyz:346654], [xyz:343372]] to [xyz:346654, xyz:343372] in groovy. I tried to apply the solution provided on the thread which to use the collectEntries() method or sum() in groovy. But instead of getting [xyz:346654, xyz:343372] which is my desired result, I get [xyz:343372]. Can someone help figure out this please? Thanks in advance!!

[xyz:346654, xyz:343372] in groovy is the same same as [xyz:343372], because of non-unique key
println ( [xyz:346654, xyz:343372] )
prints [xyz:343372]

Related

How can I add multiple GridBagLayout attributes in the 'Constraints' section of an element in Groovy (2.5.5)?

This is driving me mad at the moment, if anyone can help it would be much appreciated!! This is simple enough in Java, but when called from groovy I cannot get multiple gbc properties defined in a single constraint.
I have read on a couple of old posts on the net that GridBagConstraints properties such as gridx etc can be added as follows from here.
code snippet of interest:
label(text:'Username', constraints:gbc(gridx:0,gridy:0,gridwidth:2))
However this won't work for me and I didn't expect it to as the syntax appears to be from years ago so I assume an old API. (error message below when I try the above)
Caught: java.lang.IllegalArgumentException: cannot add to layout: constraint must be a string (or null)
I can't see how this could work as as surely the format needs to be:
GridBagConstraints gbc = new GridBagConstraints()
label("Username: ", constraints:gbc.gridx=0)
The two lines of code above run, but then I have the problem that I can't add more than one entry in the 'constraints:' section, and obviously I need to add 'gridy=0' etc.
Has anybody got any solution on how this should work?
Thanks
Taylor.

add quotes("") to a value in groovy

Even after googling a lot i couldn't find the solution for the query to which i'm going to ask so, please don't mind if its a silly question since i'm new to programming and this "Groovy" so here is my question.
I'm writing a script in groovy where it reads a file contains data like this
[a,b,c,d,e]
I want to find the length/size, as groovy is not recognizing it as string i'm getting exceptions so, i need the above one as below i.e.
[a,b,c,d,e] ---> ["a","b","c","d","e"]
please provide me the solution or let me know a way to achieve it.
Thanks in advance.
In groovy, easy to perform operations on strings. Added a file sample.txt which contains [a, b, c, d, e], for converting this to List<String>, used collect as
new File('../sample.txt').getText('UTF-8')
.tokenize(',[]').collect { it as String }.asList().size()
Online IDE: https://ideone.com/06k6Nj

In Groovy, how do I get the list of parameter names for a given job?

In Groovy, I get my hands on a Hudson job:
def job = hudson.model.Hudson.instance.getItem("Rsync library to docs-stage")
Now I would like to have a list of the parameter names for that job. Sounds simple, but Googling around isn't getting me there yet. Could someone enlighten me?
Thanks.
I'n not sure what you mean by "parameter names." As a last resort, you can get the configuration of the job as an XML String:
print job.configFile.asString()
Since this is XML, you can parse it as needed. Depending on what parameter names you are looking for, you can probably also get them directly in Java. Can you post the XML for what you are looking for? That will help in commenting on a better way.
For the parameters needed by this poster, it is easy to get using Groovy:
def params = job.getProperty('hudson.model.ParametersDefinitionProperty')
.parameterDefinitions
params.each { p -> println "$p.name"}

How to find text between two markers

I've got the below string and want to find and then join together everything in between '^[#n' and '$,s)'. these two markers may occur more than one time in our string. thanks in advance for your help.
string="O8d:^[#nI just $,s):<#Rh9f^[#n don't know $,s)>jwU*/#'^[#nhow to write this code $,s){<3f9(f3#"
for example,here the output is:
I just don't know how to write this code
You can use re.findall with a lazy quantifier:
print("".join(re.findall(r'\^\[#n(.*?)\$,s\)', string)))

Scanner to TextIO question

I need to add a value and input it directly into an array using TextIO.
I know that if I was using Scanner I would use the in.nextDouble() function, but how would I do it using TextIO?
TextIO.put("Enter credit hours:");
creditHour[sem][grd]+=in.nextDouble(); //I have to use TextIO here.
Thanks in advance,
Found it.
array [i][r] = TextIO.getlnDouble();

Resources