## 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

# List Assignment

In much the same way as scalar values, list values may be assigned to variables:

`    (\$fred, \$barney, \$dino) = ("flintstone", "rubble", undef);`

All three variables in the list on the left get new values, as if you did three separate assignments. Since the list is built up before the assignment starts, this makes it easy to swap two variables’ values in Perl:[67]

```    (\$fred, \$barney) = (\$barney, \$fred); # swap those values
(\$betty[0], \$betty[1]) = (\$betty[1], \$betty[0]);```

But what happens if the number of variables (on the left side of the equals sign) isn’t the same as the number of values (from the right side)? In a list assignment, extra values are silently ignored. Perl figures that if you wanted those values stored somewhere, you would have told it where to store them. Alternatively, if you have too many variables, the extras get the value `undef`.[68]

```    (\$fred, \$barney) = qw< flintstone rubble slate granite >; # two ignored items
(\$wilma, \$dino)  = qw[flintstone];                        # \$dino gets undef```

Now that you can assign lists, you could build up an array of strings with a line of code like this:[69]

`    (\$rocks[0], \$rocks[1], \$rocks[2], \$rocks[3]) = qw/talc mica feldspar quartz/;`

But when you wish to refer to an entire array, Perl has a simpler notation. Just use the at sign (`@`) before the name of the array (and no index brackets after it) to refer to the entire array at once. You can read this as “all of the,” so `@rocks` is “all of the rocks.”[70] This works on either side of the assignment ...

## 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