1. Write a program that acts like cat, but reverses the order of the output line
ID: 3832492 • Letter: 1
Question
1. Write a program that acts like cat, but reverses the order of the output lines. (Some systems have a utility like this named tac.) If you run yours as ./tac fred barney betty, the output should be all of file betty from last line to first, then barney and then fred, also from last line to first. (Be sure to use the ./ in your program’s invocation if you call it tac so that you don’t get the system’s utility instead!)
print reverse <>;
2. Write a program that asks the user to enter a list of strings on separate lines, printing each string in a right-justified, 20-character column. To be certain that the output is in the proper columns, print a “ruler line” of digits as 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 should give output something like this:
123456789012345678901234567890123456789012345678901234567890
hello
good-bye
what i have so far:
print "Enter some lines, then press Ctrl-Z: ";
chomp(my @lines = );
print "1234567890" x 7, "12345 "; # ruler line to column 75
foreach (@lines) {
printf "%20s ", $_;
}
3. Modify the previous program to let the user choose the column width so that entering 30, hello, good-bye (on separate lines) would put the strings at the 30th column. (Hint: see the section in Chapter 2 about controlling variable interpolation.) For extra credit, make the ruler line longer when the selected width is larger.
What I have so far.:
print "What column width would you like? ";
chomp(my $width = );
print "Enter some lines, then press Ctrl-D: "; # or Ctrl-Z
chomp(my @lines = );
print "1234567890" x (($width+9)/10), " "; # ruler line as needed
foreach (@lines) {
printf "%${width}s ", $_;
}
Explanation / Answer
Question-1:
Code:
#!/usr/bin/perl
use strict;
use warnings;
# script name: tac.pl
# program to reverse the lines in given input. Similar to tac command
my (@lines, $i);
print "Enter some lines, then press Ctrl-D: ";
while(<>){
push @lines, $_;
}
for($i = scalar @lines - 1; $i >= 0; $i--){
print $lines[$i];
}
Execution and output:
186590cb0725:Perl bonkv$ ./tac.pl
Enter some lines, then press Ctrl-D:
Write a program that acts like cat
reverses the order of the output lines
output should be all of file betty from last line to first
don’t get the system’s utility instead
don’t get the system’s utility instead
output should be all of file betty from last line to first
reverses the order of the output lines
Write a program that acts like cat
186590cb0725:Perl bonkv$
Question-2:
Code:
#!/usr/bin/perl
use strict;
use warnings;
# script name: right_just.pl
# script to right justify the given set of lines
my @lines = ();
print "Enter some lines, then press Ctrl-D: ";
while(<>){
push @lines, $_;
}
# printing ruler line to column 80 characters
print "1234567890" x 8 , " "; # ruler line to column 80 characters
for(my $i = 0; $i < scalar @lines; $i++){
printf "%20s ", $lines[$i];
}
Execution and output:
Enter some lines, then press Ctrl-D:
186590cb0725:Perl bonkv$ ./right_just.pl
movie name is killer
how are you
where are you
why do you think
12345678901234567890123456789012345678901234567890123456789012345678901234567890
movie name is killer
how are you
where are you
why do you think
186590cb0725:Perl bonkv$
Question-3:
Code:
#!/usr/bin/perl
use strict;
use warnings;
# script name: right_just_width.pl
# script to right justify the given set of lines
print "Enter column width: ";
my $width = <STDIN>;
print "Enter some lines, then press Ctrl-D: ";
my @lines = ();
while(<>){
push @lines, $_;
}
# printing ruler line to column 80 characters
print "1234567890" x (($width+9)/10), " "; # ruler line to column 80 characters
for(my $i = 0; $i < scalar @lines; $i++){
printf "%20s ", $lines[$i];
}
Execution and output:
186590cb0725:Perl bonkv$ ./right_just_width.pl
Enter column width: 100
Enter some lines, then press Ctrl-D:
movie name is killer
how are you
where are you
why do you think
1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
movie name is killer
how are you
where are you
why do you think
186590cb0725:Perl bonkv$ ./right_just_width.pl
Enter column width: 10
Enter some lines, then press Ctrl-D:
movie
abc
12
1234567890
movie
abc
12
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.