-
Notifications
You must be signed in to change notification settings - Fork 0
/
notes_part_1.txt
108 lines (92 loc) · 3.64 KB
/
notes_part_1.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
---------- Notes on Chapter 4 of Beginning Perl (Simon Cozens, 2000). ----------
To check if something exists in a data set, simply use the "exists" keyword:
if (not exists $rates{$to}) {
die "That input is invalid!\n"; }
________________________________________________________________________________
Boolean logic:
-An empty string, "", is false.
-The number zero and the string "0" are both false.
-0.0, or any variation of 0 other than simply 0, is true.
-An empty list, (), is false.
-The undefined value is false.
-Everything else is true.
________________________________________________________________________________
String comparison:
-($a gt $b) $a sorts alphabetically after $b
-($a le $b) $a sorts alphabetically before $b
-($a eq $b $)a is the same as $b
-($a ne $b) $a is not the same as $b
________________________________________________________________________________
Check if a variable is defined with the "defined" keyword:
if (defined $a);
________________________________________________________________________________
Perl features an "unless" command:
unless (exists $rates($to)) {
die "That input is invalid!\n";
________________________________________________________________________________
Else/elseif syntax:
if (condition){
action
} elseif (condition2){
action2
} else {
# everything else failed
action3
}
________________________________________________________________________________
While loop syntax:
while (condition) {action};
Also valid:
{action} while (condition)
Example:
my $count = 5;
print "Counting down: ";
while ($count > 0) {
print "$count ";
count--;
} # prints "5 4 3 2 1 "
________________________________________________________________________________
ARGV: Argument vector, takes arguments passed via command line
________________________________________________________________________________
Do-while loops also valid a la C/C++.
Syntax:
do {actions} while (condition)
________________________________________________________________________________
"Until" loops - unique to Perl:
until (condition) {statement};
Example:
my $count = 5;
until ($count-- == 0) {
print "$count ";
} # prints "5 4 3 2 1 "
________________________________________________________________________________
To break out of a loop, use the keyword "last".
Example:
my @array = ("a","b","c","stop","d");
for (@array) {
if ($_ eq "stop") {last;}
print "$_ ";
} # prints "a b c "
________________________________________________________________________________
To skip an iteration of a loop, use the "next" keyword.
Example:
my @array = ("a","b","c","skip","d");
for (@array) {
if ($_ eq "skip") {next;}
print "$_ ";
} # prints "a b c d "
________________________________________________________________________________
To redo an iteration of a loop, use the "redo" keyword.
________________________________________________________________________________
Loops can be labeled, ie. given a name before the for, while, or until.
The convention is that these labels are formed with all uppercase letters.
Example:
my @array = ("keep", "going", "stop", "now", "what");
OUTERLOOP: for (@array) {
print "$_ ";
last OUTERLOOP if ($_ eq "stop");
} # prints "keep going stop "
________________________________________________________________________________
Goto LABEL:
Valid command, but - as in other languages - not good practice.
________________________________________________________________________________