Unterminated <> operator

Hello. I'm self teaching myself and coded a program but I keep getting a illegal division by 0 error. I know what it means but I don't know where I am messing up.

Here is the code:

                     #!/usr/bin/perl
  @lines = <>;
  my %earned;
  my %possible;
  
  for ($i = 1; $i < scalar @lines; $i++) {
      $line = @lines[$i];
      chomp($line);
      @fields = split(/,/, $line);
  
      $earned{@fields[0]} = $earned{@fields[0]} + @fields[3];
      $possible{@fields[0]} = $possible{@fields[0]} + @fields[4];
  }
  print "Student\tPercent\tLetter\n";
  for $student (keys %earned) {
  

      $homework = ($earned{$_,"Homework"} / $possible{$_,"Homework"}*.10). "\n";              #This is where I am getting the error. 
      $lab = ($earned{$_,"Lab"} / $possible{$_,"Lab"}*.30) . "\n";
      $final = ($earned{$_,"Final"} / $possible{$_,"Final"}*.15) . "\n";
      $quiz = ($earned{$_,"Quiz"} / $possible{$_,"Quiz"}*.40) . "\n";
      $survey = ($earned{$_,"Survey"} / $possible{$_,"Survey"}*.30) . "\n";
      $score = ($homework + $lab + $final + $quiz +$survey);
  
      if ($score >= 90){
          $letter = "A";
              }
      elsif ($score >=80 && $score < 90){
          $letter = "B";
      }
      elsif ($score >= 70 && $score < 80){
          $letter = "C";
      }
      elsif ($score >= 60 && $score < 70){
          $letter = "D";
      }
      else {
          $letter = "E";
      }
      printf("%s\t%.2f\t%s\n", $student, $score, $letter);
  
  } 

 
 

Here is the sample data from the .csv file that this script is calculating.

Sam     Homework     95     100
Jim      Quiz              60     100
Pam     Homework     55     100
Sam     Final              80     100

Sam     Lab               5        5

FWIW,

I always check the denominator in a division operation in code before the actually division operation to insure there are never any "division by zero" errors.

In a brief glance as your code, I see you do not necessarily follow this coding practice.

The denominator $possible works as I had an original bit of code that I did beforehand to make sure it does work.

I'm afraid, no. You've been shown in your other thread that division by 0 is possible in your context and the reason therefor. And, looking at your sample data, I see it applies here as well. Print out the denominator array elements just before every division operation and report back.

1 Like

You are absolutely correct I apologize. Before I modified my code I had tested a different variation of my denominator and it had worked but when I made a change it stopped. I do however think I found where the issue lies. As I started testing the denominator, I found if I just print

$possible

it works. When I changed it to

$possible{"Sam","Homework}

it would print all of the "Homework" score for Sam. Note: Sam was a name in the .csv file listed in $0. But when I changed it to

$possible{$_,"Homework},

I was getting the illegal division by zero. So the issue has to do with the

$_

.

So my thinking on

$possible{$_, "Homework"}

was that the scalar

$possible

would calculate Sam's (the names are $0 of the .csv file) homework score (in $1 of csv file) and then use that as the denominator.

What results would you expect for "Jim", "Homework" , or "Pam", "Quiz" , with your sample data in post #1?

I'm sorry I thought I typed it out better in the original post. I will fix that. The .csv file contains 116 lines containing the grades for 5 Students. Each student has multiple Homework, Labs and Quizzes and one Survey and final. So with that line in-particular, it was to calculate all of the "homework" for each student individually and then calculate the weighted grade.

I originally was going to post a screen shot of some of the .csv file but in one of my original posts when I first joined I was advised not to post any pictures.

So basically there are 5 students total that looks like the following.

Sam,Homework,H01,19,100       

Sam,Homework,H02,82,100       

Sam,Homework,H03,95,100       

Sam,Homework,H04,46,100       

Sam,Homework,H05,82,100       

Sam,Homework,H06,97,100       

Sam,Homework,H07,52,100       

Sam,Lab,L01,41,100       

Sam,Lab,L02,85,100       

Sam,Lab,L03,99,100       

Sam,Lab,L04,99,100       

Sam,Lab,L05,0,100       

Sam,Lab,L06,0,100       

Sam,Lab,L07,0,100       

Sam,Quiz,Q01,91,100       

Sam,Quiz,Q02,85,100       

Sam,Quiz,Q03,33,100       

Sam,Quiz,Q04,64,100       

Sam,Quiz,Q05,54,100       

Sam,Quiz,Q06,95,100       

Sam,Quiz,Q07,68,100       

Sam,Final,FINAL,58,100       

Sam,Survey,WS,5,5         

 
    table \{ \}tr \{ \}col \{ \}br \{ \}td \{ padding-top: 1px; padding-right: 1px; padding-left: 1px; color: black; font-size: 12pt; font-weight: 400; font-style: normal; text-decoration: none; font-family: Calibri, sans-serif; vertical-align: bottom; border: medium none; white-space: nowrap; \}