Array index seems to not work in a log.info line OR I have a syntax error

I had been stuck trying to access elements in an byte array. I was using log.info to understand the array elements the code log.info "xarray[2] $xarray[2]" (see below) would log the whole array then the [2] as seemingly another log output.

Further below I found I could access my array element using an intermediate variable.

The Question is:

Is there a special syntax for using array[i] in log.info or is just not a valid operation?

Thanks
John

 log.info "length  $xarray.length"
            log.info "xarray[2] $xarray[2]"

Results in :




log.info "length  $xarray.length"
            byte atest = xarray[2]
            log.info "xarray[2] $atest"

Works correctly, results in:

I always use ${} around variables within a string for logging , try:
log.info "xarray[2] ${xarray[2]}"

That's my recommendation, too! :smiley: The curly braces can be left off in some cases, but it's always OK to include them. All that's happening above is that Groovy decided your to-be-interpolated expression was done once it reached the square brackets, so the those brackets--plus everything else--were considered just part of your actual string; the curly braces will tell it exactly what you mean.

Thank you both for your informative replies. Knowing why goes a long way in sinking into my mind :slight_smile:

1 Like