Summary

Today was list day. As you learned, a list is just a bunch of scalars, separated by commas and surrounded by parentheses. Assign it to an array variable @array, and you can then get at the individual elements of that array using array access notation $array[index]. You also learned about finding the last index in the array ($#array), and the number of elements in the array ($elements = @array). We finished up that section with a couple of notes on list syntax and assignment, which allows you to assign variables to values in parallel in lists on either side of an assignment expression.

After arrays, we tackled context, which allows Perl to evaluate different things differently in either scalar or list context. You learned the three questions for figuring out context: What context is expected in an expression? What data have you given it? What is that data supposed to do in that context?

Finally, we finished up with more information about simple input and output using lists. Tomorrow, we'll complete your basic list education by talking about hashes. (Actually, way up on Day 19, “Working with References,” we'll get into more advanced data structures, so we're not completely done with lists yet, but lists, arrays, and hashes will get you quite a lot for your Perl repertoire).

The built-in functions you learned about today (see the perlfunc man page for more details about these functions) include

  • qw takes a list of strings, separated by spaces, and returns a list of individual string elements. qw allows you to avoid typing a lot of quote marks and commas when you have a long list of strings to define.

  • push and pop add and delete elements from the end of a list or array, respectively.

  • defined takes a variable or list location and returns true if that location has a value other than the undefined value.

  • undef takes a variable or list location and assigns it the undefined value. With no arguments, undef simply returns the undefined value, which means it can be used to refer to that value.

  • delete undefines an element in an array, or, if that element is the last element in the array, deleted it altogether.

  • exists tests for the existence of an element.

  • sort takes a list as an argument and sorts that list in ASCII order, returning the sorted list. Sort in numeric order with the statement { $a <=> $b } just before the list.

  • scalar evaluates a list in a scalar context.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset