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

I had been stuck trying to access elements in an byte array. I was using to understand the array elements the code "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 or is just not a valid operation?

John "length  $xarray.length"
   "xarray[2] $xarray[2]"

Results in : "length  $xarray.length"
            byte atest = xarray[2]
   "xarray[2] $atest"

Works correctly, results in:

I always use ${} around variables within a string for logging , try: "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