#!/usr/bin/perl use CGI use File::Copy; use strict; use warnings; # define names of student reg info file,student count file and name of js file to be sritten out $sdata="/home/hampton1/data/student.dat"; $scnt="/home/hampton1/data/student_count.dat"; $jscd="/home/hampton1/data/jscd.js"; # open existing student reg file open(SDATA,"$sdata")|| die("Could not open file!"); # Put file into array @line = ; # delete existing count & js file - so they can be to be recreated if (-e "/home/hampton1/data/student_count.dat") { unlink($scnt); } if (-e "/home/hampton1/data/jscd.js") { unlink($jscd); } # create file(> symbol is overwriting) open (SCNT,">$scnt"); # set all permissions chmod 0777, $scnt or die "Couldn't chmod $scnt: $!"; # create file(> symbol is overwriting) open (JSCD,">$jscd"); # set all permissions chmod 0777, $jscd or die "Couldn't chmod $jscd: $!"; # Parse fields in first line ($empty, $dtereg, $timereg, $c1,$c1tot, $c2, $c2tot) = split(/\|/,$line[0]); # Load c1tot array from first record @clsid=$c1; @cqty = $c1tot; if ($c2 ne " ") { push (@clsid,$c2); push (@cqty,$c2tot); } @line = pop(@line); close SDATA; foreach $listitem (@line) { # remove CR/LF chop $listitem; # parse fields in each subsequent line ($empty, $dtereg, $timereg, $c1,$c1tot, $c2, $c2tot) = split(/\|/,$listitem); # see if class exists in array already if (grep {$_ eq $c1} @clsid) { $idx = indexArray($c1,@clsid); $cqty[$idx]+= +$c1tot; } # else add to array else { push (@clsid,$c1); push (@cqty,$c1tot); $idx = indexArray($c1,@clsid); } # See if class (2) exists in array already if (grep {$_ eq $c2} @clsid) { $idx = indexArray($c2,@clsid); $cqty[$idx]+= +$c2tot; } # else add class 2 to array else { push (@clsid,$c2); push (@cqty,$c2tot); $idx = indexArray($c2,@clsid); } } $inum = 0; # find last element # (# elements in array0 $idxcid = $clsid; # print result to student count file while ($inum <= $idxcid) { print SCNT "$clsid[$inum]\|"; print SCNT "$cqty[$inum]\n"; $inum = ++$inum; } close SCNT; sub indexArray{for(1..@_){$_[0]eq$_[$_]&&return$_-1}-1} # print results to javascript file print JSCD "function getarray ()\n"; print JSCD "{\n"; print JSCD "jclsid = []\n"; $inum = 0; # find last element # (# elements in array) $idxcid = $clsid; while ($inum <= $idxcid) { print JSCD "jclsid[$inum] = \"$clsid[$inum]\"\n"; $inum = ++$inum; } print JSCD "jclscnt = []\n"; $inum = 0; # find last element # (# elements in array) $idxcid = $clsid; while ($inum <= $idxcid) { print JSCD "jclscnt[$inum] = $cqty[$inum]\n"; $inum = ++$inum; } print JSCD "}\n"; close JSCD; # copy js file to public_html dir where all other js file are # is this needed??? $newjscd ='/home/hampton1/public_html/jscd.js"; copy($jscd, $newjscd) or die "File cannot be copied.";