Blame


1 8f7f2f4a 2021-12-17 jrmu ================================================================================
2 8f7f2f4a 2021-12-17 jrmu
3 8f7f2f4a 2021-12-17 jrmu Arrays
4 8f7f2f4a 2021-12-17 jrmu
5 8f7f2f4a 2021-12-17 jrmu An array is a list of scalars. The list can include numbers, strings,
6 8f7f2f4a 2021-12-17 jrmu references, and file handles. Arrays start with the sigil @. For example:
7 8f7f2f4a 2021-12-17 jrmu
8 8f7f2f4a 2021-12-17 jrmu my @langs = ("perl", "C", "ksh");
9 8f7f2f4a 2021-12-17 jrmu
10 8f7f2f4a 2021-12-17 jrmu Here's the array @langs in table format:
11 8f7f2f4a 2021-12-17 jrmu
12 8f7f2f4a 2021-12-17 jrmu Index | Value
13 8f7f2f4a 2021-12-17 jrmu ------+-------
14 8f7f2f4a 2021-12-17 jrmu 0 | "perl"
15 8f7f2f4a 2021-12-17 jrmu 1 | "C"
16 8f7f2f4a 2021-12-17 jrmu 2 | "ksh"
17 8f7f2f4a 2021-12-17 jrmu
18 8f7f2f4a 2021-12-17 jrmu The first element in the array, "perl", has index 0. The second element
19 8f7f2f4a 2021-12-17 jrmu "C" has index 1, and the third element "ksh" has index 2. Notice that
20 8f7f2f4a 2021-12-17 jrmu the first element starts at index 0, not 1.
21 8f7f2f4a 2021-12-17 jrmu
22 8f7f2f4a 2021-12-17 jrmu To get the first element "perl" in @langs, we write: $langs[0].
23 8f7f2f4a 2021-12-17 jrmu To get the second element "C" in @langs, we write: $langs[1].
24 8f7f2f4a 2021-12-17 jrmu To get the third element "ksh" in @langs, we write: $langs[2].
25 8f7f2f4a 2021-12-17 jrmu
26 8f7f2f4a 2021-12-17 jrmu Notice that when we retrieve a scalar from an array, the sigil changes to $.
27 8f7f2f4a 2021-12-17 jrmu If we want to get the entire array, we use the sigil @: @langs.
28 8f7f2f4a 2021-12-17 jrmu
29 8f7f2f4a 2021-12-17 jrmu To find the length of an array, use scalar(). scalar(@langs) is equal to 3.
30 8f7f2f4a 2021-12-17 jrmu
31 8f7f2f4a 2021-12-17 jrmu ================================================================================
32 8f7f2f4a 2021-12-17 jrmu
33 8f7f2f4a 2021-12-17 jrmu Push and Pop
34 8f7f2f4a 2021-12-17 jrmu
35 8f7f2f4a 2021-12-17 jrmu push(@array, LIST) lets you add LIST to the end of @array.
36 8f7f2f4a 2021-12-17 jrmu
37 8f7f2f4a 2021-12-17 jrmu my @feedURLs;
38 8f7f2f4a 2021-12-17 jrmu push(@feedURLs, "http://example.com/rss.xml");
39 8f7f2f4a 2021-12-17 jrmu
40 8f7f2f4a 2021-12-17 jrmu We add the URL to the end of @feedURLs and increase its length by 1.
41 8f7f2f4a 2021-12-17 jrmu
42 8f7f2f4a 2021-12-17 jrmu pop(@array, LIST) gives you the last element and removes it from @array.
43 8f7f2f4a 2021-12-17 jrmu
44 8f7f2f4a 2021-12-17 jrmu my $url = pop(@feedURLs);
45 8f7f2f4a 2021-12-17 jrmu process($url);
46 8f7f2f4a 2021-12-17 jrmu
47 8f7f2f4a 2021-12-17 jrmu We grab the last URL from @feedURLs, remove it from the array, and
48 8f7f2f4a 2021-12-17 jrmu then assign it to $url. Then, we process ($url).
49 8f7f2f4a 2021-12-17 jrmu
50 8f7f2f4a 2021-12-17 jrmu ================================================================================
51 8f7f2f4a 2021-12-17 jrmu
52 8f7f2f4a 2021-12-17 jrmu foreach Loops
53 8f7f2f4a 2021-12-17 jrmu
54 8f7f2f4a 2021-12-17 jrmu foreach my $lang (@langs) {
55 8f7f2f4a 2021-12-17 jrmu print "Learn $lang, ";
56 8f7f2f4a 2021-12-17 jrmu }
57 8f7f2f4a 2021-12-17 jrmu
58 8f7f2f4a 2021-12-17 jrmu The foreach loop will *iterate* through each element in the array.
59 8f7f2f4a 2021-12-17 jrmu In this loop, it will get each string in the array @langs, replace
60 8f7f2f4a 2021-12-17 jrmu $lang with the string, then print "Learn $lang, "
61 8f7f2f4a 2021-12-17 jrmu
62 8f7f2f4a 2021-12-17 jrmu The above code will output: Learn perl, Learn C, Learn ksh,
63 8f7f2f4a 2021-12-17 jrmu
64 8f7f2f4a 2021-12-17 jrmu ================================================================================
65 8f7f2f4a 2021-12-17 jrmu
66 8f7f2f4a 2021-12-17 jrmu Hashes
67 8f7f2f4a 2021-12-17 jrmu
68 8f7f2f4a 2021-12-17 jrmu A hash is like an array, except the index is called a key, and this key
69 8f7f2f4a 2021-12-17 jrmu is a string instead of a number.
70 8f7f2f4a 2021-12-17 jrmu
71 8f7f2f4a 2021-12-17 jrmu my %feedURLs = (
72 8f7f2f4a 2021-12-17 jrmu "undeadly" => "http://undeadly.org/cgi?action=rss",
73 8f7f2f4a 2021-12-17 jrmu "eff" => "https://www.eff.org/rss/updates.xml",
74 8f7f2f4a 2021-12-17 jrmu "hackernews" => "https://news.ycombinator.com/rss",
75 8f7f2f4a 2021-12-17 jrmu );
76 8f7f2f4a 2021-12-17 jrmu
77 8f7f2f4a 2021-12-17 jrmu Key | Value
78 8f7f2f4a 2021-12-17 jrmu -------------+-------------------------------------
79 8f7f2f4a 2021-12-17 jrmu "undeadly" | "http://undeadly.org/cgi?action=rss"
80 8f7f2f4a 2021-12-17 jrmu "eff" | "https://www.eff.org/rss/updates.xml"
81 8f7f2f4a 2021-12-17 jrmu "hackernews" | "https://news.ycombinator.com/rss"
82 8f7f2f4a 2021-12-17 jrmu
83 8f7f2f4a 2021-12-17 jrmu A hash contains *key-value pairs* because each key stores a value.
84 8f7f2f4a 2021-12-17 jrmu A hash is sometimes called a dictionary in other languages, because
85 8f7f2f4a 2021-12-17 jrmu the key-value pairs are similar to how a dictionary contains
86 8f7f2f4a 2021-12-17 jrmu terms and their definitions.
87 8f7f2f4a 2021-12-17 jrmu
88 8f7f2f4a 2021-12-17 jrmu Unlike an array, the keys of a hash are not numbers, so there is no real
89 8f7f2f4a 2021-12-17 jrmu order to the key-value pairs.
90 8f7f2f4a 2021-12-17 jrmu
91 8f7f2f4a 2021-12-17 jrmu To refer to a hash itself, we use the sigil %. But if we lookup the
92 8f7f2f4a 2021-12-17 jrmu value of a key, we use a $ because we're referring to a scalar.
93 8f7f2f4a 2021-12-17 jrmu $feedURLs{undeadly} will give us the value of the key "undeadly"
94 8f7f2f4a 2021-12-17 jrmu
95 8f7f2f4a 2021-12-17 jrmu ================================================================================
96 8f7f2f4a 2021-12-17 jrmu
97 8f7f2f4a 2021-12-17 jrmu Dumping Data
98 8f7f2f4a 2021-12-17 jrmu
99 8f7f2f4a 2021-12-17 jrmu When working with arrays and hashes, you may want to view the data stored
100 8f7f2f4a 2021-12-17 jrmu inside to help with debugging. We recommend using the module Data::Dumper
101 8f7f2f4a 2021-12-17 jrmu to dump the contents of arrays and hashes in a structured format:
102 8f7f2f4a 2021-12-17 jrmu
103 8f7f2f4a 2021-12-17 jrmu use Data::Dumper <data::Dumper>;
104 8f7f2f4a 2021-12-17 jrmu
105 8f7f2f4a 2021-12-17 jrmu Any time you want to dump an array or hash, pass a reference to a
106 8f7f2f4a 2021-12-17 jrmu hash or array:
107 8f7f2f4a 2021-12-17 jrmu
108 8f7f2f4a 2021-12-17 jrmu warn Dumper \@array;
109 8f7f2f4a 2021-12-17 jrmu warn Dumper \%feedURLs;
110 8f7f2f4a 2021-12-17 jrmu
111 8f7f2f4a 2021-12-17 jrmu For example:
112 8f7f2f4a 2021-12-17 jrmu
113 8f7f2f4a 2021-12-17 jrmu warn Dumper \%feedURLs;
114 8f7f2f4a 2021-12-17 jrmu
115 8f7f2f4a 2021-12-17 jrmu $VAR1 = {
116 8f7f2f4a 2021-12-17 jrmu 'undeadly' => 'http://undeadly.org/cgi?action=rss',
117 8f7f2f4a 2021-12-17 jrmu 'eff' => 'https://www.eff.org/rss/updates.xml',
118 8f7f2f4a 2021-12-17 jrmu 'hackernews' => 'https://news.ycombinator.com/rss',
119 8f7f2f4a 2021-12-17 jrmu }
120 8f7f2f4a 2021-12-17 jrmu
121 8f7f2f4a 2021-12-17 jrmu ================================================================================
122 8f7f2f4a 2021-12-17 jrmu
123 8f7f2f4a 2021-12-17 jrmu Hashes: Keys and Values
124 8f7f2f4a 2021-12-17 jrmu
125 8f7f2f4a 2021-12-17 jrmu We use keys() to get a list of all the keys in a hash.
126 8f7f2f4a 2021-12-17 jrmu keys(%feedURLs) will give us an array with 3 elements:
127 8f7f2f4a 2021-12-17 jrmu ('undeadly', 'eff', 'hackernews')
128 8f7f2f4a 2021-12-17 jrmu
129 8f7f2f4a 2021-12-17 jrmu Note: keys in the hash have no predictable order to them.
130 8f7f2f4a 2021-12-17 jrmu
131 8f7f2f4a 2021-12-17 jrmu We use values() to get a list of all the values in a hash.
132 8f7f2f4a 2021-12-17 jrmu The values will match the same order as the keys. So
133 8f7f2f4a 2021-12-17 jrmu values(%feedURLs) will give us an array with 3 elements:
134 8f7f2f4a 2021-12-17 jrmu ('http://undeadly.org/cgi?action=rss',
135 8f7f2f4a 2021-12-17 jrmu 'https://www.eff.org/rss/updates.xml',
136 8f7f2f4a 2021-12-17 jrmu 'https://news.ycombinator.com/rss')
137 8f7f2f4a 2021-12-17 jrmu
138 8f7f2f4a 2021-12-17 jrmu ================================================================================
139 8f7f2f4a 2021-12-17 jrmu
140 8f7f2f4a 2021-12-17 jrmu News Bot
141 8f7f2f4a 2021-12-17 jrmu
142 8f7f2f4a 2021-12-17 jrmu Open up ~/thirdbot and follow the instructions to set up your third bot.
143 8f7f2f4a 2021-12-17 jrmu
144 8f7f2f4a 2021-12-17 jrmu ================================================================================