Solutions# 2

advertisement
Solutions-Assignments (chapters 4-6)
1. Write a subroutine, named total, that returns the total of a list of numbers. It
reads numbers from the STDIN each number on separate line.
use strict;
use warnings;
sub sum {
my $total = 0;
foreach (@_) {
$total +=
$_;
}
return $total;
}
my @list;
while (<>) {
my $number = $_;
chomp ($number);
@list = (@list, $number);
print @list;
}
my $sum_of_list = &sum (@list);
print $sum_of_list;
2. Write a subroutine, called &above_average, that takes a list of numbers and
returns the ones that are above the average (mean).
use strict;
use warnings;
sub above_the_average {
my $total = 0;
my $number_of_items =@_;
foreach (@_) {
$total += $_;
}
my $average = $total/$number_of_items;
print "average equal to $average\n";
my @above = ();
foreach (@_) {
if ($_ > $average ) {
@above = (@above ,$_);
}
}
return @above;
}
my @list = ();
print "Enter another variable or CTRL+D\n";
while (<>) {
chomp ($_);
print "Enter a variable or CTRL+D to abort\n";
@list = (@list , $_);
}
my @above = &above_the_average (@list);
print "the following elements are above the average
@above";
3. Write a subroutine called greet, that welcomes the person you
name by telling them the name of the last person it greeted:
greet
greet
greet
greet
(“Fred”);
(“Barney”);
(“Wilma”);
(“Betty”);
This sequence of statements should print:
Hi
Hi
Hi
Hi
Fred! You are the
Barney!I’ve seen:
Wilma! I’ve seen:
Betty! I’ve seen:
first one here!
Fred
Fred Barney
Fred Barney Wilma
use strict;
use warnings;
sub greet {
my $size = @_;
my @name = ();
@name= shift;
print "Hi @name! You are the first one here!\n";
foreach (@_) {
print "Hi $_! I've seen @name!\n";
@name = (@name, $_);
}
}
my @total_names = ();
while (<>) {
chomp ($_);
@total_names = (@total_names, $_);
}
my @list = &greet (@total_names);
4. Write a program that asks the user to enter a column width (right-justified
character column), then a list of strings on separate lines, printing each string in
a right-justified, user entered-character column. To be certain that the output is
in the proper columns, print a “rule line” of digits we well. (this is simply a
debugging aid.) Make sure that you’re not using a 19-character column by
mistake! For example, entering hello, good-bye give output something like this:
Enter column width:
20
Enter strings on separate lines:
John
Travis
Jordon
Ctrl-Z
12345678901234567890123456789
John
Travis
Jordon
use strict;
use warnings;
my @names = ();
print "enter column width=\n";
chomp (my $length = <STDIN>);
print "enter each string on a separate line:\n";
while (<STDIN>) {
chomp ($_);
@names = ($_, @names);
}
my @list = (0..9);
my $temp = $length;
while ($temp/10 != 0){
print @list;
$temp -= 10;
}
print "\n";
my $format = "%${length}s\n" x @names;
printf $format , @names;
5. Write a program that will enter list of first names-last names (hint: each sting on
separate line) then, it asks the user for a given name and report the
corresponding family name.
use strict;
use warnings;
print "Enter list of fist names & last names, one entry at
each separate line,\n";
print "And don't forget to enter the first name of the
person you want me to search for his last name!\n";
my @names =();
while (<>) {
chomp ($_);
@names = (@names, $_);
}
my $first_name = pop @names;
my %hash = (@names);
my $key;
my $value;
while (( $key, $value) = each %hash) {
print
}
if (exists
print "the
\n";
}
else {
print
"$key => $hash{$key}\n";
$hash {$first_name}){
last_name of $first_name is $hash{$first_name }
"this name doesn't exist in our database!\n";
}
6. Write a program that reads a series of words (with one word
per line) until end-of-input, then prints a summary of how
many times each word was seen. (Hint: remember that when an
undefined value is used as if it were a number, perl
automatically converts it to 0.)
use strict;
use warnings;
print "Enter list of names!\n";
my @names =();
while (<>) {
chomp ($_);
@names = (@names, $_);
}
my %hash;
foreach (@names) {
if (exists $hash{$_}) {
$hash {$_} =$hash {$_} +1; }
else {
$hash {$_} =1;
}
}
my $key;
my $value;
foreach $key (sort keys %hash ) {
print "$key => $hash{$key}\n";
}
Download