Ok, I ran the script through perltidy and eliminated the global variables, yet the number of questions in the questions array is still -1 when I access the script more than once. So, when the questions are numbered, instead of starting with 1, it begins with the next number after the last time the questions were displayed. What more can I do? Here is the cleaned up code:
#!/usr/bin/perl use CGI qw(:standard); use CGI::Carp qw(fatalsToBrowser); # use strict; use warnings; # use diagnostics; #use vars # qw($fullname $anslength @allselections $file $fname $lname $time $r +eviewtime $idinput $id $courselocation $useripaddress $questionno @lo +g $clocktime $log $type $value @answers $buttonname %selections @opti +ons); my ( @explain, @allanswers, @allquestions, $fullname, $anslength, @allselections, $file, $fname, $lname, $time, $reviewtime, $idinput, $id, $courselocation, $useripaddress, $questionno, @log, $clocktime, $log, $type, $value, @answers, $buttonname, %selections, @options ); # my @allquestions; # my @allanswers; # my @explain; print header; # ********************************* DEFINE VARIABLES $time = param('time'); $reviewtime = param('reviewtime'); $file = param('file'); $idinput = param('idinput'); $fullname = param('fullname'); $fname = param('fname'); $lname = param('lname'); $courselocation = substr( $file, 0, 6 ); $useripaddress = $ENV{REMOTE_ADDR}; $questionno = 0; print "<HTML><HEAD>"; print "<TITLE>Exam</TITLE>"; print "</head>"; # ******************* START SUBROUTINE MAKEEXAM sub makeexam() { local $/ = "~\n"; # ** CHANGES INPUT RECORD SEPARATOR TO ~line +break # ************************** THIS SECTION CREATES THE HTML FOR THE + EXAM. chop($fname); $fname = lc($fname); $lname = lc($lname); $fname = ucfirst($fname); $lname = ucfirst($lname); # ********************************** RECORD ACCESS TO THE EXAM $clocktime = localtime; @log = split( /\//, $file ); @log = split( /\./, $log[1] ); $log = $log[0]; $log = $log . ".log"; open ACCESS, ">>$log" || die "Cannot open $log: $!"; print ACCESS $idinput . " " . $lname . " " . $fname . " " . $clocktime . " " . $useripaddress . " " . $time . "\n"; close(ACCESS); # ********************************** END SCRIPT TO RECORD ACCESS T +O THE EXAM print "<H2>Hello $fullname!</H2><P>"; print start_form ( -name => 'makequiz', -method => 'POST', -action => "http://ist221.nsm.tridenttech.edu/perl/grad +equiz.cgi" ); chdir("/perl/web"); open FILE, "$file" || die "Cannot open $file: $!"; while (<FILE>) { # ** READS EACH LINE OF TEXT FROM FILE chomp; # ** BY DEFAULT, CHOMPS OFF $/ FROM EACH LINE ( $type, $value ) = split( /:#\s*/, $_ ); # ***** SPLITS EACH LINE BY TYPE AN +D CONTENT if ( $type =~ m/i/i ) { # ** CHECKS FOR IMAGE print "<IMG SRC=http://ist221.tridenttech.edu/images/$courselocation/$value> +"; print "<P>"; } elsif ( $type =~ m/q/i ) { # ** CHECKS FOR QUESTION LINE & PRINTS QUESTION $questionno++; print "<B>$questionno. $value</B><BR>"; push( @allquestions, $value ); } elsif ( $type =~ m/a/i ) { # ** CHECKS FOR ANSWER LINE @answers = split( /`\s*/, $value ); $anslength = @answers; # ** LENGTH OF @ANSWERS push( @allanswers, $value ); } elsif ( $type =~ m/s/i ) { # ** CHECKS FOR SELECTION LINE $buttonname = $type . $questionno; push( @allselections, $value ); %selections = split( /`\s*/, $value ); @options = keys(%selections); if ( $anslength > 1 ) { # ** DETERMINES TO USE RADIO OR + CHECKBOX print "<BR>"; print checkbox_group( -name => $buttonname, -values => \@options, -linebreak => 'true', -labels => \%selections ); } else { print "<BR>"; print radio_group( -name => $buttonname, -values => \@options, -linebreak => 'true', -labels => \%selections, -default => '-' ); } print "<BR><HR><BR>"; } elsif ( $type =~ m/e/i ) { # ** CHECKS FOR EXPLAINATION LIN +E push( @explain, $questionno, $value ); } # END ELSIF STATEMENTS } # ** END WHILE STATEMENT close(FILE); print "<CENTER>"; # ** THIS SECTION SENDS THREE ARRAYS TO THE GRADEQUIZ.CGI print hidden( -name => "answers", -default => \@allanswers ); print hidden( -name => "questions", -default => \@allquestions ); print hidden( -name => "selections", -default => \@allselections ) +; print hidden( -name => "explain", -default => \@explain ); print hidden( -name => 'fname', -value => $fname ); print hidden( -name => 'lname', -value => $lname ); print hidden( -name => 'reviewtime', -value => $reviewtime ); print hidden( -name => 'file', -value => $file ); print hidden( -name => 'id', -value => $idinput ); print submit ( -value => 'Grade' ); print "</CENTER>"; print end_form; } # ************************* END SUBROUTINE MAKEEXAM print end_html; makeexam(); print "The last element in the questions array is $#allquestions<p>";

In reply to Re^2: Can't seem to reinitalize arrays by chriso
in thread Can't seem to reinitalize arrays by chriso

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.