[4.12] 練習問題1
1.数値のリストを受け取って、その合計を返すサブルーチン&totalを書いてください
ただし、メイン処理以外のサブルーチンのみを書けばいいようです。
メイン処理は「はじめてのPerl」から写してと、、、
プログラムはこちら。
#! /usr/local/bin/perl # ex4-1sample program use strict; use warnings; 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:\n"; my $user_total = &total(<STDIN>); print "The total of those numbers is $user_total.\n"; sub total{ my @list = @_; my $ans; foreach (@list){ $ans += $_; } $ans; }
出力結果はこちら。
The total of @fred is 25.
Enter some numbers on separate lines:
1
2
3
4
5
6
7
8
9
10
^Z
The total of those numbers is 55.