## With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

No credit card required

### 3.7. Variable Interpolation of Arrays

Like scalars, array values may be interpolated into a double-quoted string. A single element of an array will be replaced by its value, like so:

```@fred = ("hello","dolly");
\$y = 2;
\$x = "This is \$fred[1]'s place";     # "This is dolly's place"
\$x = "This is \$fred[\$y-1]'s place";  # same thing```

Note that the index expression is evaluated as an ordinary expression, as if it were outside a string. It is not variable interpolated first.

If you want to follow a simple scalar variable reference with a literal left square bracket, you need to delimit the square bracket so it isn't considered part of the array, as follows:

```@fred = ("hello","dolly");  # give value to @fred for testing
\$fred = "right";
# we are trying to say "this is right[1]"
\$x = "this is \$fred[1]";    # wrong, gives "this is dolly"
\$x = "this is \${fred}[1]";  # right (protected by braces)
\$x = "this is \$fred"."[1]"; # right (different string)
\$x = "this is \$fred\[1]";   # right (backslash hides it)```

Similarly, a list of values from an array variable can be interpolated. The simplest interpolation is an entire array, indicated by giving the array name (including its leading @ character). In this case, the elements are interpolated in sequence with a space character between them, as in:

```@fred = ("a","bb","ccc",1,2,3);
\$all = "Now for @fred here!";
# \$all gets "Now for a bb ccc 1 2 3 here!"```

You can also select a portion of an array with a slice:

`@fred = ("a","bb","ccc",1,2,3); \$all = "Now for @fred[2,3] ...`

## With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

No credit card required