Untitled

„ Use element access to initialize a list:
$fred[0] = “yabba”;
$fred[1] = “dabba”;
$fred[2] = “doo”;
„ Subscript expression can be any integer-yielding
value:
$n = 2;
print $fred[$n]; # prints $fred[2]
print $fred[$n / 3]; # truncates 2/3 to 0, thus $fred[0]
„ Maximum element index available:
$max = $#fred; # $max = 2
$last = $fred[$#fred]; # always last element
„ Out-of-bound subscripts return undef:
defined $fred[17]; # false
„ Assigning beyond end of array just stretches it:
$fred[17] = "hello";
„ Negative subscripts count back from end:
$fred[-1] # last element
$fred[-2] # second to last element
„  List of scalars, separated by commas, enclosed in parens: (1, 2, 3) # same as (1, 2, 3, )
("fred", 4.5)
( ) # empty list
(1..100) # same as (1, 2, 3, ... up to ... , 98, 99, 100)
(0..$#fred) # all indicies
„  Quoted words:
qw(fred barney betty wilma)
qw[fred barney betty wilma]
„  Works with “paired” delimiters
„  Or duplicate non-paired delimiters:
qw|foo bar|
„  Corresponding values are copied:
($fred, $barney, $dino) = ("flintstone", "rubble", undef);
„ Too short on right? Extras get undef:
($fred, $barney, $dino) = qw(flintstone rubble);
„ Too short on left? Extras are ignored
„  Imagine assigning to consecutive array elements:
($rocks[0], $rocks[1], $rocks[2], $rocks[3]) = qw(talc mica feldspar
quartz);
„  Simpler: “all of the”
@rocks = qw(talc mica feldspar quartz);
„  Previous value is always completely erased
„  Can also be used on right side of assignment:
($a, $b, $c, $d) = @rocks;
@his_rocks = @rocks;
@her_rocks = ("diamond", @rocks, "emerald");
„  Remove end of array:
@numbers = (1..10);
$final = pop @numbers; # $final gets 10
„  Add to end of array:
push @numbers, 10..15;
„  Add to beginning of array:
unshift @numbers, -10..0;
„  Remove from beginning of array:
$minus_ten = shift @numbers;
„  pop and shift are destructive, removing single element
„ Single elements act like scalars:
@rocks = qw(flintstone slate rubble);
print "barney $rocks[2]\n"; # barney rubble\n
„ “all of the” inserts spaces:
print "got @rocks\n"; # got flintstone slate rubble\n
„ Beware email addresses in double quotes:
print "My email address is [email protected]\n";
„ Precede @ with \ to avoid interpolation
„  Simplest way to walk a list:
foreach $rock (qw(bedrock slate lava)) {
print "One rock is $rock\n";
}
„  $rock is set to each element in the list in turn
„  Any outer $rock is unaffected
„  Assigning to $rock affects the element:
@rocks= qw(bedrock slate lava);
foreach (@rocks) {
$_ = "hard $_";
print "One rock is @rocks\n";
}
--------------------------------------------------One rock is hard bedrock slate lava
One rock is hard bedrock hard slate lava
One rock is hard bedrock hard slate hard lava
@rocks = qw(bedrock slate lava);
foreach $rock (@rocks) { $rock = "hard $rock" }
„  Leaving variable off uses $_
foreach (@rocks) { $_ = "hard $_" }
„  Aside: $_ is often the default for many operations
@rocks= qw(bedrock slate lava);
foreach (@rocks) {
$_ = "hard $_";
}
print "One rock is @rocks\n”;
-------------------------------------------------------One rock is hard bedrock hard slate hard lava
„  Reverse:
@rocks = qw(bedrock slate rubble granite);
@reversed = reverse @rocks;
@rocks = reverse @rocks;
„  Sort (stringwise):
@rocks = sort @rocks;
@rocks = reverse sort @rocks;
„  Default sort is not numeric:
@result = sort 97..102; # 100, 101, 102, 97, 98, 99
„  Numeric and other user-defined sorts covered later
„  Important to recognize when Perl needs which:
42 + something # looking for a scalar
sort something # looking for a list
„  because some things return different values
@people = qw(fred barney betty);
@sorted = sort @people; # @people returns elements
$number = 42 + @people; # @people returns 3 (count)
„  Even assignment itself has context:
@copy = @people; # list assignment (elements)
$count = @people; # scalar assignment (count)
„  Scalar context: one line at a time, undef at EOF
„  List context: all remaining lines:
@lines = <STDIN>;
„  Kill those newlines:
chomp(@lines = <STDIN>);
„  Once read, we’re at EOF
No more use of <STDIN> in that invocation
„  Makes an entire list in memory
Not good for 4GB web logs
@userinput = <STDIN>;
foreach (@userinput) {
print;
}
Ctrl + D (Unix)
Ctrl +Z (Windows)