具有包含点(.)的变量名的Groovy列表被转换为字符串 [英] Groovy list with variable name containing a dot (.) gets converted to string

查看:1812
本文介绍了具有包含点(.)的变量名的Groovy列表被转换为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在groovy中有一个列表,定义为

I have a list in groovy, defined as

env.list = ["abc","def"]

如果我尝试在for循环中使用它

If I try using this in a for loop

for (letters in env.list) {
  print("Letter is $letters")
}

它将遍历每个字母并打印以下内容-

It will iterate over each letter and print the following -

Letter is [
Letter is "
Letter is a
.....

如果我将列表定义如下-

If I define the list as follows -

list = ["abc","def"]

它将视为一个列表. for循环将显示以下内容.

It will treat this as a list. The for loop will print the following.

Letter is abc
Letter is def

使用groovy运行我的Jenkins管道.

Was using groovy to run my Jenkins pipeline.

  1. 为什么根据名称有区别?
  2. 我们如何使用带点(.)的变量名定义列表?

推荐答案

在jenkins管道中,env-是保存环境变量列表的变量:

in jenkins pipeline the env - is a variable that holds list of environment variables:

https://jenkins.io/doc/book/管道/jenkinsfile/#using-environment-variables

并且环境变量只能包含一个字符串

and environment variable could hold only a string

因此,当您尝试为环境变量分配一个列表时-它会自动转换为字符串

so when you try to assign to environment variable a list - it automatically converted to string

env.list = ["abc","def"]

等同于

env.list = ["abc","def"].toString()

然后您按字符迭代字符串...

and then you are iterating string by char...

这篇关于具有包含点(.)的变量名的Groovy列表被转换为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆