Unterminated <> operator


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Unterminated <> operator
# 1  
Old 11-09-2019
division by 0

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:
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.


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

Sam     Lab               5        5


Last edited by Scrutinizer; 11-10-2019 at 04:15 AM.. Reason: Extra code tags
# 2  
Old 11-09-2019
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.
# 3  
Old 11-09-2019
The denominator $possible works as I had an original bit of code that I did beforehand to make sure it does work.
# 4  
Old 11-10-2019
Quote:
Originally Posted by Eric7giants
The denominator $possible works ...

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.

Last edited by RudiC; 11-10-2019 at 07:46 AM..
This User Gave Thanks to RudiC For This Post:
# 5  
Old 11-10-2019
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
Code:
$possible

it works. When I changed it to
Code:
$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
Code:
$possible{$_,"Homework},

I was getting the illegal division by zero. So the issue has to do with the
Code:
$_

.


So my thinking on
Code:
$possible{$_, "Homework"}

was that the scalar
Code:
$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.
# 6  
Old 11-10-2019
What results would you expect for "Jim", "Homework", or "Pam", "Quiz", with your sample data in post #1?
# 7  
Old 11-10-2019
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.
Code:
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; }
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Sed: -e expression #1, char 20: unterminated address regex

I am trying to add word in last of particular line. the same command syntex is running on prompt. but in bash script give error."sed: -e expression #1, char 20: unterminated address regex" Please help. for i in `cat servername`; do ssh -q -t root@$i sed -i '/simple_allow_groups =/s/$/,... (4 Replies)
Discussion started by: yash_message
4 Replies

2. Shell Programming and Scripting

Sed: -e expression #1, char 16: unterminated address regex

I am trying to grep for a particular text (Do action on cell BL330) in a text file(sample.gz) which is searched in the content filtered by date+timestamp (2016-09-14 01:09:56,796 to 2016-09-15 04:10:29,719) on a remote machine and finally write the output into a output file on a local machine. ... (23 Replies)
Discussion started by: rbadveti
23 Replies

3. Shell Programming and Scripting

Unterminated Regular Expression

It is ok when I send send one arguement to $TILL in the awk expression. first(){ TILL=$1 echo $TILL awk '/:\\/ {P=0} P {print $0 "<br>"} FNR==1{printf("File %s:<br>", FILENAME)} /'$TILL'\/ {P=1} ' ${dirlist } } echo "<table border = '1'>" echo '<tr><td> </td><td>' ;... (3 Replies)
Discussion started by: alvinoo
3 Replies

4. UNIX for Dummies Questions & Answers

sed error unterminated `s' command

I have list of data I have cut down to format: I am using sed command to remove the sed 's/ Returns error: sed: -e expression #1, char 5: unterminated `s' command Full code line is: cat textFile | cut -d ' ' -f 4 | cut ':' -f 1 | sed 's/ Thanks, Please use next time code tags... (2 Replies)
Discussion started by: maximus73
2 Replies

5. Shell Programming and Scripting

sed: -e expression #1, char 21: unterminated `s' command

I have read many threads, but I still didn't find the right answer. May be i didn't find the right thread, though are so many threads for the same question. Basically the situation is - find date in a file and replace it with another date. (its not homework, its part of lot of a big processing,... (10 Replies)
Discussion started by: avinthm
10 Replies

6. Shell Programming and Scripting

Unterminated quoted string

Hello! I wroted a little script that should check for new updates on a server and get them if any. The problem is, every time I run it with sh, I'm getting an "script: 20: Syntax error: Unterminated quoted string" error! The problem is, there isn't any "unterminated quoted string" in my script:... (2 Replies)
Discussion started by: al0x
2 Replies

7. Shell Programming and Scripting

[Solved] sed - query - unterminated suddenly

Hi, I have been trying to get various sed statements to work. I thought I had cracked it when suddenly I start recieving these messages "sed: -e expression #1, char 14: unterminated 's' command" It happens on any sed statement I now run. The only thing I have done inbetween trying to get... (0 Replies)
Discussion started by: mcclunyboy
0 Replies

8. Shell Programming and Scripting

sed unterminated `s' command?

Hi there, I'm pretty new to this whole scripting thing. I've written myself something which takes my txt file of SMSes (the backup from the phone), and puts them into an email format, saving them as .eml files. I've tested and uploaded a batch to gmail, but because of threading issues, I've... (1 Reply)
Discussion started by: donnacha
1 Replies

9. Shell Programming and Scripting

Syntax error: Unterminated quoted string

I keep having problems when exicuting this file. It always gives me the error message "36: Syntax error: Unterminated quoted string" If someone could help me edit this it would be much appreciated. #!/bin/sh # # This will continue adding numbers # untill the total is greater than 1,000 #... (5 Replies)
Discussion started by: evilSerph
5 Replies

10. UNIX for Dummies Questions & Answers

Unterminated string

While running a shell script i am getting this warning but the script is working fine.while running the blocks of the scripts individually its running fine.But while pasting it combinedly this is the warning..wat may be the reason behind this and how to resolve it (3 Replies)
Discussion started by: dr46014
3 Replies
Login or Register to Ask a Question