Chapter 2-3 Flashcards
(21 cards)
== or eq
exactly equal to
!=
not equal to
ne
not equal to
if loop
if ($hour == 12) {
print “Lunchtime!\n”;
}
if/else loop
if ($hour == 12) { print “Lunchtime!\n”; } else { print “I'm hungry.\n”; }
if/elsif/else loop
if ($hour == 12) { print “Lunchtime!\n”; } elsif ($hour == 6) { print “Dinner!\n”; } else { print “I'm hungry.\n”; }
while loop
while ($hour
for loop
for ($hour = 0; $hour
foreach loop
foreach $icount (10..1) {
print “$icount “;
}
print “Blastoff.\n”;
Functions– things that
- do something to their inputs
- then return the result
- use () to hold their goodies.
function with return value
$return_value = function_name($var1, $var2, $var3);
@icount = (1, 2, 3, 4, 5);
building a list of items
List =
an ordered collection of scalars
Array =
ordered collection of scalars
Indices always start at
0
access array element
$name[0] = “words”;
finding the index of the last element
print $#rocks; # prints 2
finding the last element in an array
$# = 'last element' OR $rocks[0] = “bedrock”; $rocks[1] = “slate”; $rocks[2] = “lava”; print $rocks[$#rocks]; # prints “lava”;
walking through a list: foreach
foreach $item (@array) {
print “I am looking at $item.\n”;
}
listing literals
@alist = (1, 2, 3); # list of three values 1, 2, and 3
qw
@alist = qw( fred barney betty wilma dino );