初めてのPerl ~4.12 練習問題~
練習問題1:数値のリストを受け取って、その合計を返すサブルー チンの作成。
#! /usr/bin/perl -w
use strict;
my @fred = qw{ 1 3 5 7 9 };
my($fred_total) = &total(@fred);print "The total of \@fred is $fred_total. \n";
print "Enter some numbers on separate lines: ";
my($user_total) = &total();
print "The total fo those numbers is $user_total \n";sub total
{
my($ans);
foreach (@_){
$ans += $_;
}
$ans;
}
練習問題2:1..1000の合計を求める。
#! /usr/bin/perl -w
use strict;
my($total) = &total(1..1000);
print "The total of 1..1000 is $total. \n";
sub total
{
my($ans);
foreach (@_){
$ans += $_;
}
$ans;
}