perl Step 1: Open any text editor (such as Notepad) and create a file named empd
ID: 3831991 • Letter: P
Question
perl
Step 1: Open any text editor (such as Notepad) and create a file named empdata.txt. Have the following information (Full Name, Age, Gender, Marital Status) about a person inside the file ONE UNDER THE OTHER [DO NOT HAVE TITLES IN THE FILE, ONLY THE DATA] Save the file.
Here is an example, how the data in the file look like.
Paul Hendrickson
35
Male
Single
Step 2: Write a Perl script to:
a. Open the above file
b. Read the lines from the file and print them on the screen with a title added. Close the file.
So when you run the script you should display the data as follows.
Full Name: Paul Hendrickson
Age: 35
Gender: Male
Marital Status: Single
Note: Nicely align the output as shown above.
Step 3: Add more code to change 2 of the items (Age and Marital Status). Script then continues and asks the question:
Do you want to change the age (Y/y or N/n): Y
User enters ‘Y’. Script then asks the question:
What is the new age? 37
User enters 37.
Then the script asks
Do you want to change the Marital Status (Y/y or N/n): Y
User enters ‘Y’.
Script then asks the question:
What is the new marital status? Married
User Enters Married.
Then the script opens the same file empdata.txt. Rewrites the updated information back to the file. Note: If The user enters ‘N’ in any of the two questions above, no change happens to the existing data item pertaining to each question.
Step 4: Open the same file again in a text editor such as Notepad. You should now see the result like this:
Paul Hendrickson
37
Male
Married
Here is a complete run:
Full Name: Paul Hendrickson
Age: 35
Gender: Male
Marital Status: Single
Do you want to change the age (Y/y or N/n): Y
What is the new age? 37
Do you want to change the Marital Status (Y/y or N/n): Y
What is the new marital status? Married
**Opened the file in Notepad. The result is:
Paul Hendrickson
37
Male
Married
Explanation / Answer
Code:
#!/usr/bin/perl
# Program to display employee records and modify given file based on question response
use strict;
use warnings;
# Variable declaration and initialization
my (@arr, $choice, $j);
my @title = ('Full Name: ', 'Age: ', 'Gender: ', 'Marital Status: ');
# opening a file in read mode
open(FH, "<", "empdata.txt") or die "file empdata.txt cannot be opened for reading: $!";
# storing all the file content in array
@arr=();
while(my $line = <FH>){
chomp($line);
push @arr, $line;
}
close(FH);
# Iterating through array contents and asking questions to change age and marital status
$j = 0;
for(my $i = 0; $i < scalar @arr; $i++){
# displaying the title and their respective values
print $title[$j++], $arr[$i], " ";
# one employ record will be completed when i value is 3. So we started asking questions
if(($i+1) % 4 == 0){
$j = 0;
print " Do you want to change the age (Y/y or N/n): ";
$choice = <STDIN>;
chomp($choice);
if( lc $choice eq 'y' ){
print " What is the new age? ";
my $age = <STDIN>;
chomp($age);
$arr[$i - 2] = $age;
}
print " Do you want to change the Marital Status (Y/y or N/n): ";
$choice = <STDIN>;
chomp($choice);
if ( lc $choice eq 'y' ){
print " What is the new marital status? ";
my $m_status = <STDIN>;
chomp($m_status);
$arr[$i] = $m_status;
}
}
}
# opening the file empdata.txt and overwriting the content with new changes
open(FH, ">", "empdata.txt") or die "file empdata.txt cannot be opened for writing: $!";
for(my $i = 0; $i < scalar @arr; $i++){
print FH $arr[$i], " ";
}
close(FH);
Execution and output:
186590cb0725:Perl bonkv$ cat empdata.txt
Paul Hendrickson
50
Male
Married
186590cb0725:Perl bonkv$ ./mod_file.pl
Full Name: Paul Hendrickson
Age: 50
Gender: Male
Marital Status: Married
Do you want to change the age (Y/y or N/n): y
What is the new age? 20
Do you want to change the Marital Status (Y/y or N/n): y
What is the new marital status? Single
186590cb0725:Perl bonkv$ cat empdata.txt
Paul Hendrickson
20
Male
Single
186590cb0725:Perl bonkv$ cat empdata.txt
Paul Hendrickson
20
Male
Single
186590cb0725:Perl bonkv$
186590cb0725:Perl bonkv$ ./mod_file.pl
Full Name: Paul Hendrickson
Age: 20
Gender: Male
Marital Status: Single
Do you want to change the age (Y/y or N/n): n
Do you want to change the Marital Status (Y/y or N/n): n
186590cb0725:Perl bonkv$ cat empdata.txt
Paul Hendrickson
20
Male
Single
I have added one more employee record to test the script and could see it is working fine without any issues. Below is the script execution and output.
186590cb0725:Perl bonkv$ vi empdata.txt
186590cb0725:Perl bonkv$ cat empdata.txt
Paul Hendrickson
20
Male
Single
Robert Hall
40
Male
Single
186590cb0725:Perl bonkv$ ./mod_file.pl
Full Name: Paul Hendrickson
Age: 20
Gender: Male
Marital Status: Single
Do you want to change the age (Y/y or N/n): n
Do you want to change the Marital Status (Y/y or N/n): n
Full Name: Robert Hall
Age: 40
Gender: Male
Marital Status: Single
Do you want to change the age (Y/y or N/n): y
What is the new age? 35
Do you want to change the Marital Status (Y/y or N/n): y
What is the new marital status? Married
186590cb0725:Perl bonkv$ cat empdata.txt
Paul Hendrickson
20
Male
Single
Robert Hall
35
Male
Married
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.