Exercise Answers

Construct regular expressions (match operators) for the following:
Develop regular expressions for the following:
Develop regular expressions for the following:

Write a perl program that creates a student record in the form used as input to the above program. Each line should contain a student name, followed by a tab (no tabs in the name are allowed), followed by a test1 grade, followed by a tab, etc. A sample output line is:
Joe Smith\t88\t92\t77\n

Your program will accept input in the form of lines that contain name, value pairs with an equal sign (=) between the name and the value. Here is a sample input file:

name = Joe Student
test1 = 86
test2 = 77
homework = 33
name = Jane Smith
test1 = 98
test2 = 35
homework = 85

for this input, the output should be this (\t is a tab):

Joe Student\t86\t77\t33
Jane Smith\t98\t35\t85

Here is one way to do this:

!/usr/bin/perl

# read in all the lines

@lines = <>;

# get rid of all newlines

chomp(@lines);

# remove junk from each line

foreach $i (@lines) {
    $i =~ s/[^=]+=\s(.*)/\1/;
}


#now loop over all lines, handling 4 at a time
for ($i=0;$i<=$#lines;$i=$i+4) {
    print join("\t",@lines[$i..$i+3]), "\n";
}