Node:List Literals, Next:, Previous:The Semantics of Arrays, Up:Arrays



List Literals

Like scalars, it is possible to write lists as literals right in your code. Of course, as with inserting string literals in your code, you must use proper quoting.

There are two primary ways to quote list literals that we will discuss here. One is using (), and the other is using what is called a quoting operator. The quoting operator for lists is qw. A quoting operator is always followed by a single character, which is the "stop character". It will eat up all the following input until the next "stop character". In the case of qw, it will use each token that it finds as an element in a list until the second "stop character" is reached. The advantage of the qw operator is that you do not need to quote strings in any additional way, since qw is already doing the quoting for you.

Here are a few examples of some list literals, using both () and the qw operator.

     ();                   # this list has no elements; the empty list
     qw//;                 # another empty list
     ("a", "b", "c",
       1,  2,  3);         # a list with six elements
     qw/hello world
       how are you today/; # another list with six elements
     

Note that when we use the (), we have to quote all strings, and we need to separate everything by commas. The qw operator does not require this.

Finally, if you have any two scalar values where all the values between them can be enumerated, you can use an operator called the .. operator to build a list. This is most easily seen in an example:

     (1 .. 100);     # a list of 100 elements: the numbers from 1 to 100
     ('A' .. 'Z');   # a list of 26 elements: the uppercase letters From A to Z
     ('01' .. '31'); # a list of 31 elements: all possible days of a month
                     #    with leading zeros on the single digit days
     

You will find the .. operator particularly useful with slices, which we will talk about later in this chapter.