@@ -17,6 +17,7 @@ winner.
17
17
18
18
The input data (stored in a file called "scores.txt") looks like this:
19
19
20
+ = for code :lang<data>
20
21
Beth Ana Charlie Dave
21
22
Ana Dave | 3:0
22
23
Charlie Beth | 3:1
@@ -63,6 +64,7 @@ Here's one way to solve that problem in Perl 6:
63
64
64
65
This produces the output:
65
66
67
+ = for code :lang<data>
66
68
Ana has won 2 matches and 8 sets
67
69
Dave has won 2 matches and 6 sets
68
70
Charlie has won 1 matches and 4 sets
@@ -126,7 +128,7 @@ C<'scores.txt'> is a I<string literal>. A string is a piece of text, and a strin
126
128
literal is a string which appears directly in the program. In this line, it's
127
129
the argument provided to C < open > .
128
130
129
- = begin code
131
+ = begin code :preamble<my $file>
130
132
my @names = $file.get.words;
131
133
= end code
132
134
@@ -165,7 +167,7 @@ In the score counting program, C<%matches> stores the number of matches each
165
167
player has won. C < %sets > stores the number of sets each player has won. Both
166
168
of these hashes are indexed by the player's name.
167
169
168
- = begin code
170
+ = begin code :preamble<my $file>
169
171
for $file.lines -> $line {
170
172
...
171
173
}
@@ -182,7 +184,7 @@ since we already called C<$file.get> once, and going all the way to the end of t
182
184
During the first iteration, C < $line > will contain the string C < Ana Dave |
183
185
3:0 > ; during the second, C < Charlie Beth | 3:1 > , and so on.
184
186
185
- = begin code
187
+ = begin code :preamble<my $line>
186
188
my ($pairing, $result) = $line.split(' | ');
187
189
= end code
188
190
@@ -202,7 +204,7 @@ and C<$result> C<3:0>.
202
204
203
205
The next two lines follow the same pattern:
204
206
205
- = begin code
207
+ = begin code :preamble<my $pairing;my $result>
206
208
my ($p1, $p2) = $pairing.words;
207
209
my ($r1, $r2) = $result.split(':');
208
210
= end code
@@ -227,14 +229,14 @@ cell C<'0'>
227
229
228
230
The program then counts the number of sets each player has won:
229
231
230
- = begin code
232
+ = begin code :preamble<my %sets;my $p1;my $p2;my $r1;my $r2>
231
233
%sets{$p1} += $r1;
232
234
%sets{$p2} += $r2;
233
235
= end code
234
236
235
237
The C < += > assignment operator is a shortcut for:
236
238
237
- = begin code
239
+ = begin code :preamble<my %sets;my $p1;my $p2;my $r1;my $r2>
238
240
%sets{$p1} = %sets{$p1} + $r1;
239
241
%sets{$p2} = %sets{$p2} + $r2;
240
242
= end code
@@ -255,7 +257,7 @@ value starting at zero. (This is I<autovivification>). After these two lines
255
257
have run for the first time, C < %sets > contains C << 'Ana' => 3, 'Dave' => 0 >> .
256
258
(The fat arrow C << => >> separates key and value in a C < Pair > .)
257
259
258
- = begin code
260
+ = begin code :preamble<my $r1;my $r2;my $p1;my $p2;my %matches;>
259
261
if $r1 > $r2 {
260
262
%matches{$p1}++;
261
263
} else {
@@ -276,7 +278,7 @@ the increment, not the incremented value. As in many other
276
278
programming languages, you can use C < ++ > as a prefix. Then it returns the
277
279
incremented value; C < my $x = 1; say ++$x > prints C < 2 > .
278
280
279
- = begin code
281
+ = begin code :preamble<my @names;my %sets;my %matches>
280
282
my @sorted = @names.sort({ %sets{$_} }).sort({ %matches{$_} }).reverse;
281
283
= end code
282
284
@@ -319,7 +321,7 @@ C<sort> sorts in ascending order, from smallest to largest. This is the
319
321
opposite of the desired order. Therefore, the code calls the C < .reverse > method
320
322
on the result of the second sort, and stores the final list in C < @sorted > .
321
323
322
- = begin code
324
+ = begin code :preamble<my @sorted;my %matches;my %sets>
323
325
for @sorted -> $n {
324
326
say "$n has won %matches{$n} matches and %sets{$n} sets";
325
327
}
@@ -374,7 +376,7 @@ to make a list of strings. This is the C<< <...> >> L<quote-words|https://docs.p
374
376
When you put words in between the < and > they are all assumed to be strings,
375
377
so you do not need to wrap them each in double quotes C << "..." >> .
376
378
377
- = begin code
379
+ = begin code :preamble<my %sets>
378
380
say "Math: { 1 + 2 }"; # Math: 3
379
381
my @people = <Luke Matthew Mark>;
380
382
say "The synoptics are: {@people}"; # The synoptics are: Luke Matthew Mark
@@ -425,13 +427,13 @@ Hint: C<%hash.keys> returns a list of all keys stored in C<%hash>.
425
427
426
428
B < Answer: > Remove the line C < my @names = $file.get.words; > , and change:
427
429
428
- = begin code
430
+ = begin code :preamble<my @names;my %sets;my %matches>
429
431
my @sorted = @names.sort({ %sets{$_} }).sort({ %matches{$_} }).reverse;
430
432
= end code
431
433
432
434
... into:
433
435
434
- = begin code
436
+ = begin code :preamble<my %sets;my %matches>
435
437
my @sorted = %sets.keys.sort({ %sets{$_} }).sort({ %matches{$_} }).reverse;
436
438
= end code
437
439
@@ -445,10 +447,10 @@ B<Answer:> Change C<@names> to C<@valid-players>. When looping through the lines
445
447
the file, check to see that C < $p1 > and C < $p2 > are in C < @valid-players > . Note that
446
448
for membership operators you can also use C < (elem) > and C < !(elem) > .
447
449
448
- = begin code
449
- ...
450
+ = begin code :preamble<my $file>
451
+ ...;
450
452
my @valid-players = $file.get.words;
451
- ...
453
+ ...;
452
454
453
455
for $file.lines -> $line {
454
456
my ($pairing, $result) = $line.split(' | ');
0 commit comments