start page | rating of books | rating of authors | reviews | copyrights

Perl Cookbook

Perl CookbookSearch this book
Previous: 4.0. Introduction Chapter 4
Arrays
Next: 4.2. Printing a List with Commas
 

4.1. Specifying a List In Your Program

Problem

You want to include a list in your program. This is how you initialize arrays.

Solution

You can write out a comma-separated list of elements:

@a = ("quick", "brown", "fox");

If you have a lot of single-word elements, use the qw() operator:

@a = qw(Why are you teasing me?);

If you have a lot of multi-word elements, use a here document and extract lines:

@lines = (<<"END_OF_HERE_DOC" =~ m/^\s*(.+)/gm);     The boy stood on the burning deck,     It was as hot as glass. END_OF_HERE_DOC

Discussion

The first technique is the most commonly used, often because only small arrays are normally initialized as program literals. Initializing a large array would fill your program with values and make it hard to read, so such arrays are either initialized in a separate library file (see Chapter 12, Packages, Libraries, and Modules ), or the values are simply read from a file:

@bigarray = (); open(DATA, "< mydatafile")       or die "Couldn't read from datafile: $!\n"; while (<DATA>) {     chomp;     push(@bigarray, $_); }

The second technique uses the qw() operator, one of the quoting operators. Along with q() , qq() , and qx() , qw() provides another way to quote values for your program. q() behaves like single quotes, so these two lines are equivalent:

$banner = 'The Mines of Moria'; $banner = q(The Mines of Moria);

Similarly, qq() behaves like double quotes:

$name   =  "Gandalf"; $banner = "Speak, $name, and enter!"; $banner = qq(Speak, $name, and welcome!);

And qx() is almost exactly like backticks; that is, it runs a command through the shell complete with variable interpolation and backslash escapes. You can't stop interpolation with backticks, but you can with qx . If you don't want Perl variables to be expanded, you can use a single-quote delimiter on qx to suppress this:

$his_host   = 'www.perl.com'; $host_info  = `nslookup $his_host`; # expand Perl variable  $perl_info  = qx(ps $$);            # that's Perl's $$ $shell_info = qx'ps $$';            # that's the new shell's $$

Whereas q() , qq() , and qx() quote single strings, qw() quotes a list of single-word strings by splitting its argument on whitespace, without variable interpolation. These are all the same:

@banner = ('Costs', 'only', '$4.95'); @banner = qw(Costs only $4.95); @banner = split(' ', 'Costs only $4.95');

All quoting operators behave like regular expression matches, in that you can select your quote delimiters, including paired brackets. All four kinds of brackets (angle, square, curly, and round ones) nest properly. That means you can easily use parentheses or braces (or the other two) without fear, provided that they match up:

@brax   = qw! ( ) < > { } [ ] !; @rings  = qw(Nenya Narya Vilya); @tags   = qw<LI TABLE TR TD A IMG H1 P>; @sample = qw(The vertical bar (|) looks and behaves like a pipe.);

If you don't want to change the quoting character, use a backslash to escape the delimiter in the string:

@banner = qw|The vertical bar (\|) looks and behaves like a pipe.|;

You may only use qw() when each separate, space-separated component is an element in the return list. Be careful not to give Columbus four ships instead of three:

@ships  = qw(Ni�a Pinta Santa Mar�a);               # WRONG @ships  = ('Ni�a', 'Pinta', 'Santa Mar�a');         # right

See Also

The "List Value Constructors" section of perldata (1); the "List Values and Arrays" section of Chapter 2 of Programming Perl ; the "Quote and Quote-Like Operators" section of perlop (1); the s/// operator in perlop (1) and Chapter 2 of Programming Perl


Previous: 4.0. Introduction Perl Cookbook Next: 4.2. Printing a List with Commas
4.0. Introduction Book Index 4.2. Printing a List with Commas