Herong's Tutorial Notes on Perl - Part A
Dr. Herong Yang, Version 4.09

perldata - Perl Data Types

Part:   1  2  3  

(Continued from previous part...)

List Value Constructors

List value constructors are used to construct list objects. A list object is almost identical to an array object. It represents an ordered list of scalars indexed by numbers starting with 0.

List objects are constructed by listing individual scalars separated by commas. You should also use parentheses to protect the constructor as a single unit in operations to avoid confusions. If you do use parentheses, they you don't have to put string literals in quotes. Some good and bad examples are: in the following examples:

   'hello'; # ok, a single scalar is also a list
   3,5,7,11; # ok
   'Mon','Tue','Wed'; # ok
   ('Mon','Tue','Wed'); # ok
   (Mon,Tue,Wed); # ok
   'Mon',2,'Apple'; # ok
   'Jan',31,'Feb',28,'Mar',31; # ok
   'Jan' 'Feb' 'Mar'; # bad, need commas to separate the elements

An individual scalar in a list object can be accessed by the subscription notation, [index]. In this case, you need parentheses to protect the list object. Also note that negative index can be used to count down from the last entry. If the index is out of the range, you are accessing a undefined scalar. Here is a sample program with list subscriptions:

#- ListObject.pl
#- Copyright (c) 1995 by Dr. Herong Yang
#
   print((3,5,7,11)[0], "\n");           # 3
   print(('hello')[0], "\n");            # hello
   print(('hello')[1], "\n");            #
   print(('hello')[-1], "\n");           # hello
   print(('Mon','Tue','Wed')[1], "\n");  # Tue  
   print((Mon,Tue,Wed)[-1], "\n");       # Wed

Once list objects are constructed, they can be used in array variable assignment operations as in the following examples:

   @messages = 'hello'; 
   @primes = 3,5,7,11;
   @vacations = 'Mon','Tue','Wed';
   @vacations = ('Mon','Tue','Wed');
   @vacations = (Mon,Tue,Wed);
   @menu = 'Mon',2,'Apple';
   @dates = 'Jan',31,'Feb',28,'Mar',31;

List objects can also be used in a hash variable assignment operation. In this case, every two individual scalars will be paired together to form a key string and the associated scalar. If the last scalar has no partner, it will become a key with a undefined scalar. Here are some examples:

   %messages = 'hello'; 
   %primes = 3,5,7,11;
   %vacations = 'Mon','Tue','Wed';
   %vacations = ('Mon','Tue','Wed');
   %vacations = (Mon,Tue,Wed);
   %menu = 'Mon',2,'Apple';
   %dates = 'Jan',31,'Feb',28,'Mar',31;
   %prices = ('Milk',1.99,'Coke',0.99,'Bread',1.49);

If a scalar object is used where a list object is expected, the scalar object will be converted into a list object with single scalar. If a list object is used where a scalar object is expected, the list object will be converted into a scalar object with the last scalar in the list.

#- ListToScalar.pl
#- Copyright (c) 1995 by Dr. Herong Yang
#
   print(1+(3,5,7,11), "\n");    # 12
   print(1+(Mon,Tue,Wed), "\n"); # 1
   print((sort 11), "\n");       # 11

Of course, list objects can be used in many other operations. We will discuss them in other chapters.

Part:   1  2  3  

Dr. Herong Yang, updated in 2006
Herong's Tutorial Notes on Perl - Part A - perldata - Perl Data Types