List Assignment

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

	($fred, $barney, $dino) = ("flintstone", "rubble", undef);
All three variables in the list on the left get new values, just as if we did three separate assignments.  Since the list is built up before the assignement starts, this makes it easy to swap two variables' values in Perl:1
	($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.2

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

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

	($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."4

	@rocks = qw/ bedrock slate lava /;
	@tiny = ();		# the empty list
	@giant = 1..1e5;		# a list with 100,000 elements
	@stuff = (@giant, undef, @giant);	# a list with 200,001 elements
	$dino = "granite";
	@quarry = (@rocks, "crushed rock", @tiny, $dino);

That last assignment gives @quarry the five-element list (bedrock, slate, lava, crushed rock, granite), since @tiny contributes zero elements to the list.  (In particular, it doesn't put an undef item into the list - but we could do that explicitly, as we did with @stuff earlier.)  It's also worth noting that an array name is replaced by the list it contains.  An array doesn't become an element in the list, because these arrays can contain only scalars, not other arrays.5

The value of an array variable that has not yet been assigned is (), the empty list.  Just as new, empty scalars start out with undef, new, empty arrays start out with the empty list.

It's worth noting that when an array is copied to another array, it's still a list assignment.  The lists are simply stored in arrays.  For example:

	@copy = @quarry;	# copy a list from one array to another

Next:  The pop and push Operators

OR

Main Menu for this topic




























[1] As opposed to languages like C, which has no easy way to do this in general.  C programmers usually resort to some kind of macro to do this, or use a variable to temporarily hold the value.

Return to the page from whence you came






























[2] Well, that's true for scalar variables.  Array variables get an empty list, as we'll see in a moment.

Return to the page from whence you came






























[3] We're cheating by assuming that the rocks array is empty before this statement.  If there were a value in $rocks[7], say, this assignment wouldn't affect that statement.

Return to the page from whence you came






























[4] Larry claims that he chose the dollar and at-sign because they can be read as $calar (scalar) and @rray (array).  If you don't get that, or remember it that way, no big deal.

Return to the page from whence you came






























[5] But when you get into more advanced Perl, you'll learn about a special kind of scalar called a reference.  That lets us make what are informally called "lists of lists", among other interesting and useful structures.  But in that one case, you're still not really storing a list into a list; you're storing a reference into an array.

Return to the page from whence you came