aboutsummaryrefslogtreecommitdiff
path: root/challenge-004/mark-senn/perl6/ch-2.p6
blob: ba084e9d94518a3966bbb00521f2357e61666a62 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# Perl Weekly Challenge - 004
# Challenge #2
#
# See
#     engineering.purdue.edu/~mark/pwc-004.pdf
# for more information.

# Run using Perl 6.
use v6;


# Read the list of letters from the
# "letters.txt" file.
# Each letter is on a separate line.
# Words and letters are matched in a
# case-insensitive way.

# Get the list of letters from letters.txt.
my @let = slurp('letters.txt').comb(/<[a..z]>/);
# Make all letters lowercase.
@let = map({.trans('A..Z' => 'a..z')}, @let);
# Make a Perl 6 bag of letters so letters and
# words can be compared easily.
my $let = bag @let;


# Read the words from the "words.txt" file
# one at a time.
# Each word is on a separate line.
# Words and letters are matched in a
# case-insensitive way.

my $fn = 'words.txt';
my $fh = open $fn;

for $fh.lines
{
    # Make all word letters lowercase.
    .trans('A..Z' => 'a..z');
    # Make a Perl 6 bag of word letters
    # so letters and word letters can
    # be compared easily.
    my $word = bag $_.comb(/<[a..z]>/);
    # If the word letters are a subset
    # of the letters specified, print the word.
    ($word (<=) $let)  and  say "$_";
}

close $fh;