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
#!/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.