================================================================================ Arrays An array is a list of scalars. The list can include numbers, strings, references, and file handles. Arrays start with the sigil @. For example: my @langs = ("perl", "C", "ksh"); Here's the array @langs in table format: Index | Value ------+------- 0 | "perl" 1 | "C" 2 | "ksh" The first element in the array, "perl", has index 0. The second element "C" has index 1, and the third element "ksh" has index 2. Notice that the first element starts at index 0, not 1. To get the first element "perl" in @langs, we write: $langs[0]. To get the second element "C" in @langs, we write: $langs[1]. To get the third element "ksh" in @langs, we write: $langs[2]. Notice that when we retrieve a scalar from an array, the sigil changes to $. If we want to get the entire array, we use the sigil @: @langs. To find the length of an array, use scalar(). scalar(@langs) is equal to 3. ================================================================================ Push and Pop push(@array, LIST) lets you add LIST to the end of @array. my @feedURLs; push(@feedURLs, "http://example.com/rss.xml"); We add the URL to the end of @feedURLs and increase its length by 1. pop(@array, LIST) gives you the last element and removes it from @array. my $url = pop(@feedURLs); process($url); We grab the last URL from @feedURLs, remove it from the array, and then assign it to $url. Then, we process ($url). ================================================================================ foreach Loops foreach my $lang (@langs) { print "Learn $lang, "; } The foreach loop will *iterate* through each element in the array. In this loop, it will get each string in the array @langs, replace $lang with the string, then print "Learn $lang, " The above code will output: Learn perl, Learn C, Learn ksh, ================================================================================ Hashes A hash is like an array, except the index is called a key, and this key is a string instead of a number. my %feedURLs = ( "undeadly" => "http://undeadly.org/cgi?action=rss", "eff" => "https://www.eff.org/rss/updates.xml", "hackernews" => "https://news.ycombinator.com/rss", ); Key | Value -------------+------------------------------------- "undeadly" | "http://undeadly.org/cgi?action=rss" "eff" | "https://www.eff.org/rss/updates.xml" "hackernews" | "https://news.ycombinator.com/rss" A hash contains *key-value pairs* because each key stores a value. A hash is sometimes called a dictionary in other languages, because the key-value pairs are similar to how a dictionary contains terms and their definitions. Unlike an array, the keys of a hash are not numbers, so there is no real order to the key-value pairs. To refer to a hash itself, we use the sigil %. But if we lookup the value of a key, we use a $ because we're referring to a scalar. $feedURLs{undeadly} will give us the value of the key "undeadly" ================================================================================ Dumping Data When working with arrays and hashes, you may want to view the data stored inside to help with debugging. We recommend using the module Data::Dumper to dump the contents of arrays and hashes in a structured format: use Data::Dumper ; Any time you want to dump an array or hash, pass a reference to a hash or array: warn Dumper \@array; warn Dumper \%feedURLs; For example: warn Dumper \%feedURLs; $VAR1 = { 'undeadly' => 'http://undeadly.org/cgi?action=rss', 'eff' => 'https://www.eff.org/rss/updates.xml', 'hackernews' => 'https://news.ycombinator.com/rss', } ================================================================================ Hashes: Keys and Values We use keys() to get a list of all the keys in a hash. keys(%feedURLs) will give us an array with 3 elements: ('undeadly', 'eff', 'hackernews') Note: keys in the hash have no predictable order to them. We use values() to get a list of all the values in a hash. The values will match the same order as the keys. So values(%feedURLs) will give us an array with 3 elements: ('http://undeadly.org/cgi?action=rss', 'https://www.eff.org/rss/updates.xml', 'https://news.ycombinator.com/rss') ================================================================================ News Bot Open up ~/thirdbot and follow the instructions to set up your third bot. ================================================================================