1

Given the following example:

def servers = "sftp.host.com:32025|GB,sftp.host.com:32029|ES,sftp.host.com:32030|PT,sftp.host.com:32027|FI," servers.split(',').each { it.split("\\|").each { println("sftp address: ${it[0]} countrycode: ${it[1]}\n") } } 

The idea was to extract some fields from a delimited list of , then get address|countryCode out from that field to process further, but the only thing i am getting out is the first letter of each field.

sftp address: s countrycode: f sftp address: G countrycode: B ...

Not sure whats going on here?

2
  • 1
    The each method doesn't work in the pipeline due to CPS transformation. Use a for-in loop instead: for(server in servers.split(',')) {…}. Alternatively use @NonCPS annotation, see example. Commented Feb 11, 2023 at 10:33
  • Does this answer your question? Why an each loop in a Jenkinsfile stops at first iteration Commented Feb 11, 2023 at 10:39

1 Answer 1

0

Working Code

def servers = "sftp.host.com:32025|GB,sftp.host.com:32029|ES,sftp.host.com:32030|PT,sftp.host.com:32027|FI," servers.split(',').each { def itParts = it.split(/\|/) println "sftp address: ${itParts[0]} countrycode: ${itParts[1]}" } 

Code Output

sftp address: sftp.host.com:32025 countrycode: GB sftp address: sftp.host.com:32029 countrycode: ES sftp address: sftp.host.com:32030 countrycode: PT sftp address: sftp.host.com:32027 countrycode: FI 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.