-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlearningperl4qna.pl
112 lines (89 loc) · 1.89 KB
/
learningperl4qna.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#!/usr/bin/perl
use strict;
use warnings;
# Q1 Chapter 4, create subroutine total.
sub total {
my $result = 0;
foreach (@_) {
$result += $_;
}
$result;
}
# With nos. given in the code.
my @fred = qw/1 3 5 7 9/;
my $fred_total = &total(@fred);
print "The total of \@fred is $fred_total\n";
# With nos. to be taken from STDIN.
# print "Enter some nos.:";
# my @user_no = <STDIN>;
# my $user_total = &total(@user_no);
# print "The total of \@user_no is $user_total\n";
# Q2 Chapter 4. Sum of the nos from 1 to 1000.
my @sum_tho = (1 .. 1000);
my $total_tho = &total(@sum_tho);
print "Total of \@sum_tho is $total_tho\n";
# Q3. nos. above the average no.
sub average {
if (@_ == 0) { return }
my $sum = 0;
my $noofele = @_;
foreach ( @_ ) {
$sum += $_;
}
$sum / $noofele;
}
sub max {
my ($max_so_far) = shift @_;
foreach (@_) {
if ($_ > $max_so_far) {
$max_so_far = $_;
}
}
$max_so_far;
}
# sub above_average {
# # my $result = &average(@_);
# my $noofele = @_;
# my $mean = &average(@_);
# if ( $mean < $noofele ) {
# ++$mean..$noofele;
# } else {
# &max(@_);
# }
# }
my @fred1 = &above_average(1..10);
print "\@fred1 is @fred1\n";
my @barney = above_average(100,1..10);
print "\@barney is @barney\n";
#sub max {
#my $result = &average(1..10);
# my $tot = &total(1..10);
#print "Average for \$tot is $result\n";
## Above average another approach.
sub above_average {
my $average = &average(@_);
my @list;
foreach my $element (@_) {
if ($element > $average ) {
push @list, $element;
}
}
@list;
}
## Question 4. subroutine to greet a person.
use 5.010;
sub greet {
state @names;
my $name = shift;
print "Hi $name!";
if ( @names ) {
print "i have seen: @names!\n";
} else {
print "you are the first person here!\n";
}
push @names, $name;
}
&greet('Fred');
&greet('Barney');
&greet('Wilma');
&greet('Betty');