2020年4月25日土曜日

開発環境

初めてのPerl 第7版 (Randal L. Schwartz(著)brian d foy(著)Tom Phoenix(著)近藤 嘉雪(翻訳)嶋田 健志(翻訳)、オライリージャパン)の4章(サブルーチン)、4.13(練習問題)1、2の解答を求めてみる。

コード

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

say '1.';

sub total {
    my $result = 0;
    for (@_) {
        $result += $_;
    }
    $result;
}

my @fred = qw{1 3 5 7 9};
my $fred_total = total(@fred);
say "The total of \@fred is $fred_total.";
print "Enter some numbers on separate lines: ";
my $user_total = total(<STDIN>);
say "The total of those numbers is $user_total.";

say "2.";
my $total = total 1..1000;
say "1から1,000までの合計は$total。";

say "おまけ";
my @nums = (1, 3, 5, 7, 9);
say total @nums;

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

% ./sample1.pl 
1.
The total of @fred is 25.
Enter some numbers on separate lines: 1
2
3
4
5
6
7
8
9
10
The total of those numbers is 55.
2.
1から1,000までの合計は500500。
おまけ
25
% ./sample1.pl
1.
The total of @fred is 25.
Enter some numbers on separate lines: 1
The total of those numbers is 1.
2.
1から1,000までの合計は500500。
おまけ
25
% ./sample1.pl 
1.
The total of @fred is 25.
Enter some numbers on separate lines: -1
1
The total of those numbers is 0.
2.
1から1,000までの合計は500500。
おまけ
25
% ./sample1.pl
1.
The total of @fred is 25.
Enter some numbers on separate lines: The total of those numbers is 0.
2.
1から1,000までの合計は500500。
おまけ
25
% 

0 コメント:

コメントを投稿