Write a Perl program to accomplish each of the following on the file solar.txt s
ID: 3813205 • Letter: W
Question
Write a Perl program to accomplish each of the following on the file solar.txt
solar.txt below
1. Print all records that do not list a discoverer in the eighth field.
2. Print every record after erasing the second field. Note: It would be better to say "print every record" omitting the second field.
3. Print the records for satellites that have negative orbital periods. (A negative orbital period simply means that the satellite orbits in a counterclockwise direction.)
4. Print the data for the objects discovered by the Voyager2 space probe.
5. Print each record with the orbital period given in seconds rather than days.
Separate, count and sort the words in the example text file, electricity.txt (see link at the class homepage). Sort in the following orders and your output should be nicely lined up in columns to the output file.
electricity.txt below
1. alphabetically (ignoring capitalization),
2. alphabetically with upper case words just in front of lower case words with the same initial characters
3. by frequency, from high to low, (any order for equal frequency)
4. by frequency, with alphabetical order for words with the same frequency
Write a perl program that replaces all digits with the name of the digit, so every "0" is replaced with "zero" , "1" is replaced with "one", ... "9" is replaced with "nine". Test your program with your own input file containing digits and letters. Your program should write the result to output file and you need to print both input and output files along with your source code.
Explanation / Answer
Hi,
Please find below the answer-
Ans 1-
use strict;
use warnings;
while (defined(my $line = <>)) {
my @chunks = split ' ', $line;
if ( $chunks[7] ne "-" ) {
print "$chunks[0] $chunks[1] $chunks[2] $chunks[3] $chunks[4] $chunks[5] $chunks[6] $chunks[7] $chunks[8] "
}
}
Ans 2-
use strict;
use warnings;
while (defined(my $line = <>)) {
my @chunks = split ' ', $line;
print "$chunks[1] $chunks[2] $chunks[3] $chunks[4] $chunks[5] $chunks[6] $chunks[7] $chunks[8] "
}
Ans 3-
use strict;
use warnings;
while (defined(my $line = <>)) {
my @chunks = split ' ', $line;
if ( $chunks[3] =~ /-(d+)/ ) {
print "$chunks[1] $chunks[2] $chunks[3] $chunks[4] $chunks[5] $chunks[6] $chunks[7] $chunks[8] "
}
}
Ans 4-
use strict;
use warnings;
while (defined(my $line = <>)) {
my @chunks = split ' ', $line;
if ( $chunks[7] =~ /Voyager2/ ) {
print "$chunks[0] $chunks[1] $chunks[2] $chunks[3] $chunks[4] $chunks[5] $chunks[6] $chunks[7] $chunks[8] "
}
}
Ans 5-
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.