Needing help trying to create this program in perl: empdata as text editor (such
ID: 3835521 • Letter: N
Question
Needing help trying to create this program in perl:
empdata as text editor (such as Notepad) and Name, Age, Gender, Marital create a file named following information (Full 1: the Step Open any txt. Have Status) about a person inside the file ONE UNDER THE OTHER [DONOT 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. Paul Hendrickson Full Name: 35 Age Male Gender: 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:Explanation / Answer
#!/usr/bin/perl
open(DATA,"<empdata.txt") or die "Can't open data";
# open and read first 4 file lines
$file[0]=<DATA>; chomp($file[0]);
$file[1]=<DATA>; chomp($file[1]);
$file[2]=<DATA>; chomp($file[2]);
$file[3]=<DATA>; chomp($file[3]);
close (DATA);
print " Full Name: $file[0] Age: $file[1] Gender: $file[2] Marital Status: $file[3] ";
print " Do you want to change the age? (Y/y or N/n): ";
my $c = 'n';
$a = <STDIN>; chomp($a);
if($a eq'Y' or $a eq 'y') {
print "What is the new age? ";
$file2[1]= <STDIN>;chomp($file2[1]);
$c=$a;
}
else
{
$file2[1] = $file[1];
}
print " Do you want to change the Marital Status? (Y/y or N/n): ";
$b = <STDIN>; chomp($b);
if($b eq 'Y' or $b eq 'y') {
print "What is the new marital status? ";
$file2[3] = <STDIN>; chomp($file2[3]);
$c=$b;
}
else
{
$file2[3] = $file[3];
}
if($c eq 'Y' or $c eq 'y') {
$file2[0]=$file[0];
$file2[2]=$file[2];
open(INFO, ">info.txt") or die "Can't open data";
print INFO join(" ",$file2[0], $file2[1], $file2[2], $file2[3]);
close INFO;
open(DATA,"<info.txt") or die "Can't open data";
$file[0]=<DATA>; chomp($file[0]);
$file[1]=<DATA>; chomp($file[1]);
$file[2]=<DATA>; chomp($file[2]);
$file[3]=<DATA>; chomp($file[3]);
close (DATA);
print "The result is: Full Name: $file[0] Age: $file[1] Gender: $file[2] Marital Status: $file[3] ";
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.