# How to define Groovy text array using a parameter?

**URL:** <https://community.jenkins.io/t/how-to-define-groovy-text-array-using-a-parameter/8337>\
**Category:** Using Jenkins\
**Created:** [July 6, 2023, 8:44am UTC](https://community.jenkins.io/t/how-to-define-groovy-text-array-using-a-parameter/8337 "2023-07-06T08:44:01Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![DavidA2014](https://dub1.discourse-cdn.com/flex013/user_avatar/community.jenkins.io/davida2014/32/88_2.png) [@DavidA2014](https://community.jenkins.io/u/DavidA2014)\
**Post date:** [July 6, 2023, 8:44am UTC](https://community.jenkins.io/t/how-to-define-groovy-text-array-using-a-parameter/8337/1 "2023-07-06T08:44:01Z")

</div>

Hi, I want to set a Groovy variable, which defines an array of strings, using a parameter.

```
parameters {
      text defaultValue: 'file1.txt,\nfile2.txt,\nfile3.txt,\nfile4.txt',
      name: 'filenames', 
}

stage('exp') {
    steps {
        script {
        //def files = ['file1.txt', 'file2.txt', 'file3.txt', 'file4.txt']
        def files = params.filenames

        for (file in files) {
             echo "Preparing $file"
        }
    }
}

```

This isn’t working. In the ‘for’ loop, variable ‘file’ just contains one character, not an entire filename.

What would be the correct syntax please?

---

<div class="post-metadata">

**Author:** ![mawinter69](https://dub1.discourse-cdn.com/flex013/user_avatar/community.jenkins.io/mawinter69/32/1625_2.png) [@mawinter69](https://community.jenkins.io/u/mawinter69)\
**Post date:** [July 6, 2023, 12:13pm UTC](https://community.jenkins.io/t/how-to-define-groovy-text-array-using-a-parameter/8337/2 "2023-07-06T12:13:34Z")

</div>

There is no parameter that will return an array to my knowledge. But you can just split the parameter at the line breaks (do not include the commas in the default)

```auto
parameters {
      text defaultValue: 'file1.txt\nfile2.txt\nfile3.txt\nfile4.txt',
      name: 'filenames', 
}

stage('exp') {
    steps {
        script {
        //def files = ['file1.txt', 'file2.txt', 'file3.txt', 'file4.txt']
        def files = params.filenames.split('\n')

        for (file in files) {
             echo "Preparing $file"
        }
    }
}

```

---

<div class="post-metadata">

**Author:** ![DavidA2014](https://dub1.discourse-cdn.com/flex013/user_avatar/community.jenkins.io/davida2014/32/88_2.png) [@DavidA2014](https://community.jenkins.io/u/DavidA2014)\
**Post date:** [July 6, 2023, 1:35pm UTC](https://community.jenkins.io/t/how-to-define-groovy-text-array-using-a-parameter/8337/3 "2023-07-06T13:35:01Z")

</div>

Thank you Markus. That worked nicely.

---

<div class="post-metadata">

**Author:** ![imoutsatsos](https://dub1.discourse-cdn.com/flex013/user_avatar/community.jenkins.io/imoutsatsos/32/162_2.png) [@imoutsatsos](https://community.jenkins.io/u/imoutsatsos)\
**Post date:** [July 6, 2023, 6:38pm UTC](https://community.jenkins.io/t/how-to-define-groovy-text-array-using-a-parameter/8337/4 "2023-07-06T18:38:43Z")

</div>

@mawinter69 For freestyle jobs, active choice parameters return array lists. Can they be used in a pipeline job for this purpose?

---

<div class="post-metadata">

**Author:** ![mawinter69](https://dub1.discourse-cdn.com/flex013/user_avatar/community.jenkins.io/mawinter69/32/1625_2.png) [@mawinter69](https://community.jenkins.io/u/mawinter69)\
**Post date:** [July 6, 2023, 7:14pm UTC](https://community.jenkins.io/t/how-to-define-groovy-text-array-using-a-parameter/8337/5 "2023-07-06T19:14:27Z")

</div>

Active choice can be used in pipeline, but configuration is a pain at the moment due to missing symbols (see [Add @Symbol to support pipeline by kinow · Pull Request #80 · jenkinsci/active-choices-plugin · GitHub](https://github.com/jenkinsci/active-choices-plugin/pull/80)). But normally parameters are passed as environment variables in freestyle jobs so I wonder how should they be used when there is an array. The active choice plugin unfortunately doesn’t explain how to e.g. a mutliselect parameter can be used in a pipeline.  
Also in this context here I think the requirement is to allow the user to enter arbitrary values and not choose from a list of predefined values

---

<div class="post-metadata">

**Author:** ![mawinter69](https://dub1.discourse-cdn.com/flex013/user_avatar/community.jenkins.io/mawinter69/32/1625_2.png) [@mawinter69](https://community.jenkins.io/u/mawinter69)\
**Post date:** [July 6, 2023, 7:29pm UTC](https://community.jenkins.io/t/how-to-define-groovy-text-array-using-a-parameter/8337/6 "2023-07-06T19:29:40Z")

</div>

taking a closer look at the sources, active choice is also just returning a string and not an array.

---

<div class="post-metadata">

**Author:** ![imoutsatsos](https://dub1.discourse-cdn.com/flex013/user_avatar/community.jenkins.io/imoutsatsos/32/162_2.png) [@imoutsatsos](https://community.jenkins.io/u/imoutsatsos)\
**Post date:** [July 7, 2023, 12:54am UTC](https://community.jenkins.io/t/how-to-define-groovy-text-array-using-a-parameter/8337/7 "2023-07-07T00:54:29Z")

</div>

Thank you for your important (and correct!) points @mawinter69 about Active Choices. The AC groovy script has to return a list or a map, which is turned into a comma delimited parameter string, when the user makes multiple selections. I think it would be relatively trivial to split this string to create an array. Nonetheless, having multiple options to do something is always the strength of Jenkins.
