2020年6月22日月曜日

開発環境

続・初めてのPerl 改訂第2版 (Randal L. Schwartz(著)brian d foy(著)Tom Phoenix(著)伊藤 直也(監修)長尾 高弘(翻訳)、オライリージャパン)の4章(リファレンス入門)、4.11(練習問題)2の解答を求めてみる。

コード

#!/usr/bin/env perl
use strict;
use warnings;
use v5.30;

say '2.';

sub check_required_items {
    my $who = shift;
    my $items = shift;

    my %whose_items = map {$_, 1 } @$items;
    my @required = qw(preserver sunscreen water_bottle jacket);
    my @missing = ();
    foreach (@required) {
        unless ($whose_items{$_}) {
            print "$who is missing $_\n";
            push @missing, $_;
        }
    }
    if (@missing) {
        print "Adding @missing to @$items for $who.\n";
        push @$items, @missing;
    }
}
sub check_items_for_all {
    my $all = shift;
    foreach my $who (keys %$all) {
        check_required_items $who, $all->{$who};
    }
}
my @gilligan = qw(red_shirt hat lucky_socks water_bottle);
my @skipper = qw(blue_shirt hat jaket preserver sunscreen);
my @professor = qw(sunscreen water_bottle slide_rule batteries radio);

my %all = (
    Gilligan => \@gilligan,
    Skipper => \@skipper,
    Professor => \@professor,
);

check_items_for_all(\%all);

入出力結果(Zsh、PowerShell、Terminal)

% ./sample2.pl
2.
Skipper is missing water_bottle
Skipper is missing jacket
Adding water_bottle jacket to blue_shirt hat jaket preserver sunscreen for Skipper.
Professor is missing preserver
Professor is missing jacket
Adding preserver jacket to sunscreen water_bottle slide_rule batteries radio for Professor.
Gilligan is missing preserver
Gilligan is missing sunscreen
Gilligan is missing jacket
Adding preserver sunscreen jacket to red_shirt hat lucky_socks water_bottle for Gilligan.
%

0 コメント:

コメントを投稿