Need assistance with perl. Thank you. 1. Make a program that prints each line of
ID: 3837775 • Letter: N
Question
Need assistance with perl. Thank you.
1. Make a program that prints each line of its input that mentions fred. (It shouldn’t do anything for other lines of input.) Does it match if your input string is Fred, frederick, or Alfred? Make a small text file with a few lines mentioning “fred flintstone” and his friends, then use that file as input to this program and the ones later in this section. The perluniprops documentation lists all of the Unicode properties and how many characters match that property.
2. Modify the previous program to allow Fred to match as well. Does it match now if your input string is Fred, frederick, or Alfred? (Add lines with these names to the text file.)
3. Make a program that prints each line of its input that contains a period (.), ignoring other lines of input. Try it on the small text file from the previous exercise: does it notice Mr. Slate?
4. Make a program that prints each line that has a word that is capitalized but not ALL capitalized. Does it match Fred but neither fred nor FRED?
5. Make a program that prints each line that has a two of the same nonwhitespace characters next to each other. It should match lines that contain words such as Mississippi, Bamm-Bamm, or llama. 6. [8] Extra credit exercise: write a program that prints out any input line that men- tions both wilma and fred.
Explanation / Answer
1. This assume that input is a file and is given on command line. Please note this will ot print lines with frederick as thats what I feel question is asking for
#!/usr/bin/perl -w
open(FILE, $ARGV[0]) or die("Could not open the file $ARGV[0]");
while ($line = <FILE>){
if($line=~/s+freds+/)
{
print $line;
}
}
close(FILE);
2.
#!/usr/bin/perl -w
open(FILE, $ARGV[0]) or die("Could not open the file $ARGV[0]");
while ($line = <FILE>){
if($line=~/s+fred|Freds+/)
{
print $line;
}
}
close(FILE);
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.