1 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:
8 8f7f2f4a 2021-12-17 jrmu my @langs = ("perl", "C", "ksh");
10 8f7f2f4a 2021-12-17 jrmu Here's the array @langs in table format:
12 8f7f2f4a 2021-12-17 jrmu Index | Value
13 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.
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].
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.
29 8f7f2f4a 2021-12-17 jrmu To find the length of an array, use scalar(). scalar(@langs) is equal to 3.
31 8f7f2f4a 2021-12-17 jrmu ================================================================================
33 8f7f2f4a 2021-12-17 jrmu Push and Pop
35 8f7f2f4a 2021-12-17 jrmu push(@array, LIST) lets you add LIST to the end of @array.
37 8f7f2f4a 2021-12-17 jrmu my @feedURLs;
38 8f7f2f4a 2021-12-17 jrmu push(@feedURLs, "http://example.com/rss.xml");
40 8f7f2f4a 2021-12-17 jrmu We add the URL to the end of @feedURLs and increase its length by 1.
42 8f7f2f4a 2021-12-17 jrmu pop(@array, LIST) gives you the last element and removes it from @array.
44 8f7f2f4a 2021-12-17 jrmu my $url = pop(@feedURLs);
45 8f7f2f4a 2021-12-17 jrmu process($url);
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).
50 8f7f2f4a 2021-12-17 jrmu ================================================================================
52 8f7f2f4a 2021-12-17 jrmu foreach Loops
54 8f7f2f4a 2021-12-17 jrmu foreach my $lang (@langs) {
55 8f7f2f4a 2021-12-17 jrmu print "Learn $lang, ";
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, "
62 8f7f2f4a 2021-12-17 jrmu The above code will output: Learn perl, Learn C, Learn ksh,
64 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.
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",
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"
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.
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.
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"
95 8f7f2f4a 2021-12-17 jrmu ================================================================================
97 8f7f2f4a 2021-12-17 jrmu Dumping Data
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:
103 8f7f2f4a 2021-12-17 jrmu use Data::Dumper <data::Dumper>;
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:
108 8f7f2f4a 2021-12-17 jrmu warn Dumper \@array;
109 8f7f2f4a 2021-12-17 jrmu warn Dumper \%feedURLs;
111 8f7f2f4a 2021-12-17 jrmu For example:
113 8f7f2f4a 2021-12-17 jrmu warn Dumper \%feedURLs;
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',
121 8f7f2f4a 2021-12-17 jrmu ================================================================================
123 8f7f2f4a 2021-12-17 jrmu Hashes: Keys and Values
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')
129 8f7f2f4a 2021-12-17 jrmu Note: keys in the hash have no predictable order to them.
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')
138 8f7f2f4a 2021-12-17 jrmu ================================================================================
142 8f7f2f4a 2021-12-17 jrmu Open up ~/thirdbot and follow the instructions to set up your third bot.
144 8f7f2f4a 2021-12-17 jrmu ================================================================================