開発環境
- macOS Mojave - Apple (OS)
- Emacs (Text Editor)
- Windows 10 Pro (OS)
- Strawberry Perl (WindowsのPerlの言語処理系)
- Visual Studio Code (Text Editor)
- Perl 5.30 (プログラミング言語)
続・初めてのPerl 改訂第2版 (Randal L. Schwartz(著)、brian d foy(著)、Tom Phoenix(著)、伊藤 直也(監修)、長尾 高弘(翻訳)、オライリージャパン)の4章(リファレンス入門)、4.11(練習問題)3の解答を求めてみる。
コード
#!/usr/bin/env perl
use strict;
use warnings;
use v5.30;
my %gilligan_info = (
name => 'Gilligan',
hat => 'White',
shirt => 'Red',
position => 'First Mate',
);
my %skipper_info = (
name => 'Skipper',
hat => 'Black',
shirt => 'Blue',
position => 'Captain',
);
my @crew = (\%gilligan_info, \%skipper_info);
my $format = "%-15s %-7s %-7s %-15s\n";
printf $format, qw(Name Shirt Hat Position);
foreach my $crewmember (@crew) {
printf $format, @$crewmember{'name', 'shirt', 'hat', 'position'};
}
my %mr_howell = (
name => 'Mr. Howell',
);
my %mrs_howell = (
name => 'Mrs. Howell',
);
push @crew, \%mr_howell, \%mrs_howell;
foreach (@crew) {
$_->{'address'} = 'The Island';
}
foreach (@crew) {
printf "%s at %s\n", $_->{'name'}, $_->{'address'};
}
foreach (@crew) {
$_->{'address'} = 'The Country Club' if $_->{'name'} =~ /Howell/;
}
foreach (@crew) {
printf "%s at %s\n", $_->{'name'}, $_->{'address'};
}
入出力結果(Zsh、PowerShell、Terminal)
% ./sample3.pl
Name Shirt Hat Position
Gilligan Red White First Mate
Skipper Blue Black Captain
Gilligan at The Island
Skipper at The Island
Mr. Howell at The Island
Mrs. Howell at The Island
Gilligan at The Island
Skipper at The Island
Mr. Howell at The Country Club
Mrs. Howell at The Country Club
%
0 コメント:
コメントを投稿