Forum Discussion

ak0032803's avatar
ak0032803
Contributor
15 years ago

Groovy Script for an array with mulitiple elements

Hi ,

I have elements like -
Planname = Plan1,Plan2
Quota = 20,30.

Logically it means -For plan 1 quota is 20 and for plan2 quota is 30.
I want to generate array like -

<Plans>
<PlanName>Plan1</PlanName>
<QuotaSize>20</QuotaSize>
<PlanName>Plan2</PlanName>
<QuotaSize>30</QuotaSize>
</Plans>

I am not able to generate such data.
I am able to split these string independently but it doesn't helped.

Is there any example groovy script to achieve it ?

2 Replies

  • M_McDonald's avatar
    M_McDonald
    Super Contributor
    Assuming that there are the same number of items in each string, how about this:

    def planNames = "Plan1,Plan2".split(",")
    def quotas = "20,30".split(",")
    def p = "<Plans>\n"
    for (i in 0..planNames.size()-1) {
    p += "<PlanName>${planNames[i]}</PlanName>\n<QuotaSize>${quotas[i]}</QuotaSize>\n"
    }
    p += "</Plans>"

    log.info p