初めてのPerl 第4章 練習問題2
初めてのPerl 第4章 練習問題2
use strict; use warnings; use 5.010; use Data::Dumper; sub total{ my @numbers = @_; my $have; foreach my $number(@numbers){ $have += $number; } return $have; } my @fred = 1..1000; 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 number is $user_total \n";
以下、出力結果となります。
the total of @fred is 500500
Enter some numbers on separate lines:
1
2
3
4
5
6
the total of those number is 21