Skip to content

Commit 1f70c83

Browse files
committed
Pass example compilation test
1 parent 979f7f9 commit 1f70c83

File tree

1 file changed

+17
-15
lines changed

1 file changed

+17
-15
lines changed

doc/Language/101-basics.pod6

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ winner.
1717
1818
The input data (stored in a file called "scores.txt") looks like this:
1919
20+
=for code :lang<data>
2021
Beth Ana Charlie Dave
2122
Ana Dave | 3:0
2223
Charlie Beth | 3:1
@@ -63,6 +64,7 @@ Here's one way to solve that problem in Perl 6:
6364
6465
This produces the output:
6566
67+
=for code :lang<data>
6668
Ana has won 2 matches and 8 sets
6769
Dave has won 2 matches and 6 sets
6870
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
126128
literal is a string which appears directly in the program. In this line, it's
127129
the argument provided to C<open>.
128130
129-
=begin code
131+
=begin code :preamble<my $file>
130132
my @names = $file.get.words;
131133
=end code
132134
@@ -165,7 +167,7 @@ In the score counting program, C<%matches> stores the number of matches each
165167
player has won. C<%sets> stores the number of sets each player has won. Both
166168
of these hashes are indexed by the player's name.
167169
168-
=begin code
170+
=begin code :preamble<my $file>
169171
for $file.lines -> $line {
170172
...
171173
}
@@ -182,7 +184,7 @@ since we already called C<$file.get> once, and going all the way to the end of t
182184
During the first iteration, C<$line> will contain the string C<Ana Dave |
183185
3:0>; during the second, C<Charlie Beth | 3:1>, and so on.
184186
185-
=begin code
187+
=begin code :preamble<my $line>
186188
my ($pairing, $result) = $line.split(' | ');
187189
=end code
188190
@@ -202,7 +204,7 @@ and C<$result> C<3:0>.
202204
203205
The next two lines follow the same pattern:
204206
205-
=begin code
207+
=begin code :preamble<my $pairing;my $result>
206208
my ($p1, $p2) = $pairing.words;
207209
my ($r1, $r2) = $result.split(':');
208210
=end code
@@ -227,14 +229,14 @@ cell C<'0'>
227229
228230
The program then counts the number of sets each player has won:
229231
230-
=begin code
232+
=begin code :preamble<my %sets;my $p1;my $p2;my $r1;my $r2>
231233
%sets{$p1} += $r1;
232234
%sets{$p2} += $r2;
233235
=end code
234236
235237
The C<+=> assignment operator is a shortcut for:
236238
237-
=begin code
239+
=begin code :preamble<my %sets;my $p1;my $p2;my $r1;my $r2>
238240
%sets{$p1} = %sets{$p1} + $r1;
239241
%sets{$p2} = %sets{$p2} + $r2;
240242
=end code
@@ -255,7 +257,7 @@ value starting at zero. (This is I<autovivification>). After these two lines
255257
have run for the first time, C<%sets> contains C<< 'Ana' => 3, 'Dave' => 0 >>.
256258
(The fat arrow C<< => >> separates key and value in a C<Pair>.)
257259
258-
=begin code
260+
=begin code :preamble<my $r1;my $r2;my $p1;my $p2;my %matches;>
259261
if $r1 > $r2 {
260262
%matches{$p1}++;
261263
} else {
@@ -276,7 +278,7 @@ the increment, not the incremented value. As in many other
276278
programming languages, you can use C<++> as a prefix. Then it returns the
277279
incremented value; C<my $x = 1; say ++$x> prints C<2>.
278280
279-
=begin code
281+
=begin code :preamble<my @names;my %sets;my %matches>
280282
my @sorted = @names.sort({ %sets{$_} }).sort({ %matches{$_} }).reverse;
281283
=end code
282284
@@ -319,7 +321,7 @@ C<sort> sorts in ascending order, from smallest to largest. This is the
319321
opposite of the desired order. Therefore, the code calls the C<.reverse> method
320322
on the result of the second sort, and stores the final list in C<@sorted>.
321323
322-
=begin code
324+
=begin code :preamble<my @sorted;my %matches;my %sets>
323325
for @sorted -> $n {
324326
say "$n has won %matches{$n} matches and %sets{$n} sets";
325327
}
@@ -374,7 +376,7 @@ to make a list of strings. This is the C<< <...> >> L<quote-words|https://docs.p
374376
When you put words in between the < and > they are all assumed to be strings,
375377
so you do not need to wrap them each in double quotes C<< "..." >>.
376378
377-
=begin code
379+
=begin code :preamble<my %sets>
378380
say "Math: { 1 + 2 }"; # Math: 3
379381
my @people = <Luke Matthew Mark>;
380382
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>.
425427
426428
B<Answer:> Remove the line C<my @names = $file.get.words;>, and change:
427429
428-
=begin code
430+
=begin code :preamble<my @names;my %sets;my %matches>
429431
my @sorted = @names.sort({ %sets{$_} }).sort({ %matches{$_} }).reverse;
430432
=end code
431433
432434
... into:
433435
434-
=begin code
436+
=begin code :preamble<my %sets;my %matches>
435437
my @sorted = %sets.keys.sort({ %sets{$_} }).sort({ %matches{$_} }).reverse;
436438
=end code
437439
@@ -445,10 +447,10 @@ B<Answer:> Change C<@names> to C<@valid-players>. When looping through the lines
445447
the file, check to see that C<$p1> and C<$p2> are in C<@valid-players>. Note that
446448
for membership operators you can also use C<(elem)> and C<!(elem)>.
447449
448-
=begin code
449-
...
450+
=begin code :preamble<my $file>
451+
...;
450452
my @valid-players = $file.get.words;
451-
...
453+
...;
452454
453455
for $file.lines -> $line {
454456
my ($pairing, $result) = $line.split(' | ');

0 commit comments

Comments
 (0)