Element Access
• Arrays are indexed numerically from 0
```my @years = ( 1066, 1776, 1984, 2001, 2112 );
print \$years;    # prints 1984
```

• Accessed elements are scalar, so they take a dollar sign.
Array slices
• Subsections of an array can be taken, called a "slice".
```my @years = ( 1066, 1776, 1984, 2001, 2112 );
my @future = @years[3,4];
# @future is now ( 2001, 2112 )
```

• Slices can be lvalues as well as rvalues
```@list[0,1] = @list[1,0];    # swaps the first two values in @list.
(\$x,\$y) = (\$y,\$x);          # swaps the values of \$x and \$y
```
Arrays as stacks
• push & modify the right-hand side of the list, and return a value.

• shift & unshift modify the left-hand side of the list, and return a value.
```my @years = ( 1066, 1776, 1984, 2001, 2112 );
my \$past = shift @years;
my \$future = pop @years;
# \$past is 1066, \$future is \$2112 and @years is ( 1776, 1984, 2001 )
```
Array functions
• sort() sorts a list
(Note that it's based on strings; we'll get into custom sort functions later)

• reverse() reverses a list

• join() glues the elements of a list together
```my @partners = qw( Dewey Cheatham Howe );
my \$firm = join( ", ", @partners );
# \$firm is now "Dewey, Cheatham, Howe";

my @countdown = (10..1);
# @countdown is now (10,9,8,7,6,5,4,3,2,1)
print join( "... ", @countdown );
# prints "10... 9... 8... 7... 6... 5... 4... 3... 2... 1"
```

• split() breaks apart a scalar into a list
```my \$today = "12/7/1941";
my @dayparts = split( "/", \$today );
# @dayparts is now ( 12, 7, 1941 );
```
Lists in scalar context
• A list in scalar context gives the length of the array
```my @list = (1776, 2001, 1984);
my @n = @list;                     # @n is now (1776, 2001, 1984)
my \$n = @list;                     # \$n is now 3
```

• Scalar comparisons force scalar context.
```my @foo = (1,2,3);
my @bar = qw( x y z );
print "Match!" if @foo == @bar; # prints Match!
```

• The scalar function explicitly forces scalar context. Using it is often not necessary, but it's usually a Good Thing.
```my @list = (1776, 2001, 1984);
my \$n = scalar @list;               # \$n is now 3
```
Index
Introduction
What Is Perl?
Perl Resources
Running a Perl Program
Perl Thinking
Data Types
Scalars
Strings: Single Quoted
Strings: Double Quoted
Scalar operations
Scalar comparisons
Variables
Lists
Using Lists
Control Structures
Hashes
Hash Manipulation
File Handling
Regex Matching
Regex Matching: Ex. 1
Regex Matching: Ex. 2
Regex Replacing
Subroutines
Anonymous subs
References
Structures
Modules
Modules: File::Find
Modules: Internet
Modules: Win32::*
Everything Else
Next page >>>