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?

Andrej Istomin
  • 2,527
  • 2
  • 15
  • 22
Rygo
  • 159
  • 1
  • 8
  • 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](https://stackoverflow.com/a/40197015/7571258). – zett42 Feb 11 '23 at 10:33
  • Does this answer your question? [Why an each loop in a Jenkinsfile stops at first iteration](https://stackoverflow.com/questions/37594635/why-an-each-loop-in-a-jenkinsfile-stops-at-first-iteration) – zett42 Feb 11 '23 at 10:39

1 Answers1

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
Terry Ebdon
  • 487
  • 2
  • 6