Blame
Date:
Fri Dec 17 14:15:53 2021 UTC
Message:
Import sources
001
2021-12-17
jrmu
================================================================================
002
2021-12-17
jrmu
003
2021-12-17
jrmu
Arrays
004
2021-12-17
jrmu
005
2021-12-17
jrmu
An array is a list of scalars. The list can include numbers, strings,
006
2021-12-17
jrmu
references, and file handles. Arrays start with the sigil @. For example:
007
2021-12-17
jrmu
008
2021-12-17
jrmu
my @langs = ("perl", "C", "ksh");
009
2021-12-17
jrmu
010
2021-12-17
jrmu
Here's the array @langs in table format:
011
2021-12-17
jrmu
012
2021-12-17
jrmu
Index | Value
013
2021-12-17
jrmu
------+-------
014
2021-12-17
jrmu
0 | "perl"
015
2021-12-17
jrmu
1 | "C"
016
2021-12-17
jrmu
2 | "ksh"
017
2021-12-17
jrmu
018
2021-12-17
jrmu
The first element in the array, "perl", has index 0. The second element
019
2021-12-17
jrmu
"C" has index 1, and the third element "ksh" has index 2. Notice that
020
2021-12-17
jrmu
the first element starts at index 0, not 1.
021
2021-12-17
jrmu
022
2021-12-17
jrmu
To get the first element "perl" in @langs, we write: $langs[0].
023
2021-12-17
jrmu
To get the second element "C" in @langs, we write: $langs[1].
024
2021-12-17
jrmu
To get the third element "ksh" in @langs, we write: $langs[2].
025
2021-12-17
jrmu
026
2021-12-17
jrmu
Notice that when we retrieve a scalar from an array, the sigil changes to $.
027
2021-12-17
jrmu
If we want to get the entire array, we use the sigil @: @langs.
028
2021-12-17
jrmu
029
2021-12-17
jrmu
To find the length of an array, use scalar(). scalar(@langs) is equal to 3.
030
2021-12-17
jrmu
031
2021-12-17
jrmu
================================================================================
032
2021-12-17
jrmu
033
2021-12-17
jrmu
Push and Pop
034
2021-12-17
jrmu
035
2021-12-17
jrmu
push(@array, LIST) lets you add LIST to the end of @array.
036
2021-12-17
jrmu
037
2021-12-17
jrmu
my @feedURLs;
038
2021-12-17
jrmu
push(@feedURLs, "http://example.com/rss.xml");
039
2021-12-17
jrmu
040
2021-12-17
jrmu
We add the URL to the end of @feedURLs and increase its length by 1.
041
2021-12-17
jrmu
042
2021-12-17
jrmu
pop(@array, LIST) gives you the last element and removes it from @array.
043
2021-12-17
jrmu
044
2021-12-17
jrmu
my $url = pop(@feedURLs);
045
2021-12-17
jrmu
process($url);
046
2021-12-17
jrmu
047
2021-12-17
jrmu
We grab the last URL from @feedURLs, remove it from the array, and
048
2021-12-17
jrmu
then assign it to $url. Then, we process ($url).
049
2021-12-17
jrmu
050
2021-12-17
jrmu
================================================================================
051
2021-12-17
jrmu
052
2021-12-17
jrmu
foreach Loops
053
2021-12-17
jrmu
054
2021-12-17
jrmu
foreach my $lang (@langs) {
055
2021-12-17
jrmu
print "Learn $lang, ";
056
2021-12-17
jrmu
}
057
2021-12-17
jrmu
058
2021-12-17
jrmu
The foreach loop will *iterate* through each element in the array.
059
2021-12-17
jrmu
In this loop, it will get each string in the array @langs, replace
060
2021-12-17
jrmu
$lang with the string, then print "Learn $lang, "
061
2021-12-17
jrmu
062
2021-12-17
jrmu
The above code will output: Learn perl, Learn C, Learn ksh,
063
2021-12-17
jrmu
064
2021-12-17
jrmu
================================================================================
065
2021-12-17
jrmu
066
2021-12-17
jrmu
Hashes
067
2021-12-17
jrmu
068
2021-12-17
jrmu
A hash is like an array, except the index is called a key, and this key
069
2021-12-17
jrmu
is a string instead of a number.
070
2021-12-17
jrmu
071
2021-12-17
jrmu
my %feedURLs = (
072
2021-12-17
jrmu
"undeadly" => "http://undeadly.org/cgi?action=rss",
073
2021-12-17
jrmu
"eff" => "https://www.eff.org/rss/updates.xml",
074
2021-12-17
jrmu
"hackernews" => "https://news.ycombinator.com/rss",
075
2021-12-17
jrmu
);
076
2021-12-17
jrmu
077
2021-12-17
jrmu
Key | Value
078
2021-12-17
jrmu
-------------+-------------------------------------
079
2021-12-17
jrmu
"undeadly" | "http://undeadly.org/cgi?action=rss"
080
2021-12-17
jrmu
"eff" | "https://www.eff.org/rss/updates.xml"
081
2021-12-17
jrmu
"hackernews" | "https://news.ycombinator.com/rss"
082
2021-12-17
jrmu
083
2021-12-17
jrmu
A hash contains *key-value pairs* because each key stores a value.
084
2021-12-17
jrmu
A hash is sometimes called a dictionary in other languages, because
085
2021-12-17
jrmu
the key-value pairs are similar to how a dictionary contains
086
2021-12-17
jrmu
terms and their definitions.
087
2021-12-17
jrmu
088
2021-12-17
jrmu
Unlike an array, the keys of a hash are not numbers, so there is no real
089
2021-12-17
jrmu
order to the key-value pairs.
090
2021-12-17
jrmu
091
2021-12-17
jrmu
To refer to a hash itself, we use the sigil %. But if we lookup the
092
2021-12-17
jrmu
value of a key, we use a $ because we're referring to a scalar.
093
2021-12-17
jrmu
$feedURLs{undeadly} will give us the value of the key "undeadly"
094
2021-12-17
jrmu
095
2021-12-17
jrmu
================================================================================
096
2021-12-17
jrmu
097
2021-12-17
jrmu
Dumping Data
098
2021-12-17
jrmu
099
2021-12-17
jrmu
When working with arrays and hashes, you may want to view the data stored
100
2021-12-17
jrmu
inside to help with debugging. We recommend using the module Data::Dumper
101
2021-12-17
jrmu
to dump the contents of arrays and hashes in a structured format:
102
2021-12-17
jrmu
103
2021-12-17
jrmu
use Data::Dumper <data::Dumper>;
104
2021-12-17
jrmu
105
2021-12-17
jrmu
Any time you want to dump an array or hash, pass a reference to a
106
2021-12-17
jrmu
hash or array:
107
2021-12-17
jrmu
108
2021-12-17
jrmu
warn Dumper \@array;
109
2021-12-17
jrmu
warn Dumper \%feedURLs;
110
2021-12-17
jrmu
111
2021-12-17
jrmu
For example:
112
2021-12-17
jrmu
113
2021-12-17
jrmu
warn Dumper \%feedURLs;
114
2021-12-17
jrmu
115
2021-12-17
jrmu
$VAR1 = {
116
2021-12-17
jrmu
'undeadly' => 'http://undeadly.org/cgi?action=rss',
117
2021-12-17
jrmu
'eff' => 'https://www.eff.org/rss/updates.xml',
118
2021-12-17
jrmu
'hackernews' => 'https://news.ycombinator.com/rss',
119
2021-12-17
jrmu
}
120
2021-12-17
jrmu
121
2021-12-17
jrmu
================================================================================
122
2021-12-17
jrmu
123
2021-12-17
jrmu
Hashes: Keys and Values
124
2021-12-17
jrmu
125
2021-12-17
jrmu
We use keys() to get a list of all the keys in a hash.
126
2021-12-17
jrmu
keys(%feedURLs) will give us an array with 3 elements:
127
2021-12-17
jrmu
('undeadly', 'eff', 'hackernews')
128
2021-12-17
jrmu
129
2021-12-17
jrmu
Note: keys in the hash have no predictable order to them.
130
2021-12-17
jrmu
131
2021-12-17
jrmu
We use values() to get a list of all the values in a hash.
132
2021-12-17
jrmu
The values will match the same order as the keys. So
133
2021-12-17
jrmu
values(%feedURLs) will give us an array with 3 elements:
134
2021-12-17
jrmu
('http://undeadly.org/cgi?action=rss',
135
2021-12-17
jrmu
'https://www.eff.org/rss/updates.xml',
136
2021-12-17
jrmu
'https://news.ycombinator.com/rss')
137
2021-12-17
jrmu
138
2021-12-17
jrmu
================================================================================
139
2021-12-17
jrmu
140
2021-12-17
jrmu
News Bot
141
2021-12-17
jrmu
142
2021-12-17
jrmu
Open up ~/thirdbot and follow the instructions to set up your third bot.
143
2021-12-17
jrmu
144
2021-12-17
jrmu
================================================================================
IRCNow