HOME HOUSE PROJECTS SCHOOL PHOTO GALLERY RESUME
Perl Source Code for Assignment M2.1

Note: this source code was designed to parse the contents of a specified type of file

 

#!/usr/bin/perl -w

use strict;

use CGI ':standard';

my ($arraysize, @temp, $temp);

my (%record, %rectemp, $record, $rectemp, $counter);

my @orderedList;

my $filepathname;

my ($textsearch, @subjects, $subjects);

my (%grouped, $group, $coursename);

my (@students, $student, @grades, $grades, @report, $report);

print 'Enter full path and filename, e.g., home/me/mydir/myfile ';

 

$filepathname = <>;

print "\n";

 

# adding search for file

open (FILE, "<$filepathname") || Error('open', 'file');

 

# read first line only

while (<FILE>)

{

# get rid of extraneous white space

chomp;

 

# now search for header

if ($_ =~m/Subjects-/){

 

 # split the header line into header and subjects

 $subjects = $';

 @subjects = split (/,/ , $subjects);

 

  # strip leading white space

  my $i=0;

  while ($i < $#subjects+1) {

    $subjects[$i] =~ s/^\s+//;

    $i++;

    }

  }

  else {

    print "\n \'Subjects-\' header not found.  Exiting program.\n";

    die "File missing header";

    }

  # exit this loop thus splitting the file into 2 parts

  last;

}

 

# read the rest of the lines

while (<FILE>){

 

  #match anything that's not a number

  /[^0-9]+/;

  #grab the student name & clean it up

  $student = $&;

  chop ($student);

 

  # push the grades for this person into a list & clean it up

  $grades = $';

  chomp ($grades);

  @grades = split(/ / , $grades);

 

  #add a reference to the grade list at the student name

  while (@grades) {

    %record = (

              'student' => $student,

              'grade' => pop(@grades),

              'subject' => $#grades+1

              );

    push (@report, {%record})

    }

}

 

# done with the file

close (FILE);

 

  # print an error message if no students were found in the file

    if (scalar (@report) <1) {

    print "\nError! No students found in this file.\n";

    die "Exiting program.\n";

    }

 

# print out the report arrays

 foreach (@report) {

  %rectemp = %$_;

  $coursename = $subjects[$rectemp{'subject'}];

  push (@{$grouped{$coursename}}, {%rectemp});

  }

 

# print the grade report, with highest grade for each class listed first

foreach $coursename (keys %grouped) {

  print "\n";

  print $coursename;

  print "\n";

  $record = $grouped{$coursename};

 @orderedList = sort {$b->{'grade'} <=> $a->{'grade'}} @$record;

 

   foreach (@orderedList) {

    print %$_->{'student'};

    print " ";

    print %$_ ->{'grade'};

    print "\n";

    }

   }

 

print "\nEnd of Program.";

print "\n";

 

sub Error {

      print "\nThe server can't $_[0] the $_[1]: $! \n";

      exit;

   }

 
 
Site comments or problems:   email joan smith