Question & Answer: Write a perl program that corrects the broken delimiting in cs371598roster.raw. Your program must print the…..

Write a perl program that corrects the broken delimiting in cs371598roster.raw. Your program must print the roster with corrected delimiting–comma followed by exactly 1 space, no space before commas. You will get 2 points extra credit if you use regular expression search and replace to correct the delimiting. Include the source code of your program in perl_assignment.txt. cs371598roster.raw: Drew,Matthew J. , s1058828 Howerth, Chloe E., s1002240 Karolewicz, Michael J., s0995867 Perzely, Connor J.,s0958005 Tanenbaum, Roberto,s1124377 Williams, Gregory M., s1186794 Guan,Tiffany , s1103462 Jaligama,Vishnu Praneeth, s1143667 Jin, Ailan , s1152308 Miceli, Paul V. , s0937120 Shukla, Prerana, s1113985 Torrenegra , Harry E., s0894879 Mathighatta Shivakumar, Meghana, s1175925 Tangirala, Venkata Naga Sai Meghana, s1087887

Expert Answer

 The problem is quite tricky, if you have a comma with no after and before space then simply replace it with comma with a space. However, if you have a comman with an before space and an after space, you just have to remove the before space. Also, if you have comma with an after space only, then it is perfect. If you have a comma with an before space only then remove the before space and add an after space. So, here is the code which has multiple replacements to keep a check on the corrected delimiting. Have a look:

#!/usr/bin/perl -w

use strict;

open(FILE, “roster.raw”) || die “File not found”; #put the filename here
my @lines = <FILE>;
close(FILE);

my @newlines;
foreach(@lines) { #for each lines
$_ =~ s/,/ ,/g; #replacing a only comma with a space before comma
$_ =~ s/ ,/, /g; #now our space before comma will be replaced by space after comma
$_ =~ s/ ,/,/g; #previous step actually made a error to the case 2 so, replace the space before comma with just comma
$_ =~ s/, /, /g; #if there are any two spaces after comma occurrence then just replace it

push(@newlines,$_);
}

print @newlines; #print the line
close(FILE);

I hope it was useful, if you have any doubts let me know in the comments.

Still stressed from student homework?
Get quality assistance from academic writers!