Perl Scripting issue - homework

 
Thread Tools Search this Thread
Homework and Emergencies Homework & Coursework Questions Perl Scripting issue - homework
# 1  
Old 05-10-2011
Perl Scripting issue - homework

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted!

1. The problem statement, all variables and given/known data:
Here is the problem:
Lesson 13 OBJ1: OST Class online.

Write a script called obj13-1.pl and a library called obj13-lib.pl.

1- The library should contain a function that takes in an array of numbers (of arbitrary size).
2- The function will then (calculate the average of the numbers), (the total of all of the numbers added together), and a new array of numbers which is comprised of the (other numbers divided by 2).
It will then return a new list with all of that information.

The script file, obj13-1.pl should get a list of numbers from the user (either via STDIN or a list of arguments) and call the library function.

2. Relevant commands, code, scripts, algorithms:



3. The attempts at a solution (include all code and scripts):
My attempt at this, remember I'm a newbie to unix/linux:

Obj13-1.pl
Code:
  #!’usr/bin/perl
  require ‘obj13-lib.pl’;
   
  @userarray = <STDIN>;
  $sum = sumIt(@userarray);
  $ave = $sum/(@userarray);
   
  print”Total: $sum\n”;
  print”Average: $ave\n”;
   
  foreach((@userarray)/2){      THIS PART IS WRONG yes I need a foreach loop to draw out the input numbers, but (@userarray)/2) isn't working. 

  print”Divided by 2: @userarray\n”;
  }
   
  #_END_

Obj13-lib.pl
Code:
  Sub sumIt{
         @functionarray = @_;
         foreach $line(@functionarray){
         $functionsum += $line;
          }
           return $funtionsum;
  }
   
  1;


4. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):
OST OReilly School of Technology, Online course, USA, Kelly Hoover instructor. Course is Unix/Linux administration.

Tried to post this and got this msg: So I'm taking the link to my class out.
  1. "You are only allowed to post URLs once you have at least 5 posts"

Note: Without school/professor/course information, you will be banned if you post here! You must complete the entire template (not just parts of it).

Last edited by pludi; 05-11-2011 at 04:04 AM..
# 2  
Old 05-16-2011
Here is correct code to make it run: solved it myself

Obj13-1.pl

Code:
  #!/usr/bin/perl
  require ‘obj13t-lib.pl’;
  
  @userarray = <STDIN>;
  $sum = sumIt(@userarray);
  $ave = $sum/(@userarray);
  $newline = @newaray;
  
  print”Total: $sum\n”;
  print”Average: $ave\n”;
  print”New Array Div by 2: @newarray\n”;
  
  #_END_


obj13-lib.pl

Code:
  sub sumIt{
         my @functionarray = @_;
         my $functionsum = 0;
         my $newline = 0;
  
  foreach my $line(@userarray){
     $functionsum += $line;
     $newline = $line/2;
     push(@newarray,$newline);     
     }
     return ($functionsum);
     }
    1;


Last edited by pludi; 05-17-2011 at 04:27 AM..
# 3  
Old 05-17-2011
Actually you've broken the spec there, your imported function from the library file should return an array with all the calculations performed, ie you should be able to call it thus
Code:
my ($average, $total, @halves) = imported_calculation_function(@ARGV);

You could create the array of halves as you loop through the array and calculate the average afterwards.
For bonus points, check for an empty array so as to avoid a divide by zero error and also ensure all elements of the array you send to the function are numeric.
Returning an array reference for the @halves array wins you another bonus, no effect here, but it's better practice as it makes the function extensible and the first assignment to an array swallows all subsequent return values.

Last edited by Skrynesaver; 05-17-2011 at 10:04 AM.. Reason: advised on potential approach
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Scripting Homework, need help

Hi I just started scripting in my Unix class, but can't understand it and have trouble with homework. This is my homework: Dean needs a daily written report of the status of his Unix computer. He wants to obtain certain information about users and processes that are only accessible by executing... (1 Reply)
Discussion started by: arbogastcarl
1 Replies

2. Shell Programming and Scripting

Scripting issue

Hi Experts, I'm stuck with a single liner bash script. Need your help to fix it. Let me brief you that what is my expectations from this script. I need the interfaces configured for a list of servers, their respective IP address and FQDN in a output file. The output should be in the same... (3 Replies)
Discussion started by: arun_adm
3 Replies

3. UNIX for Dummies Questions & Answers

Scripting issue

Hi experts... I am really new to this scripting, i really need your help. I have this script to read and print the report, however the script does not pickup the correct hostname (eg. Svtns11d).. The script will start reading/pick up on the second line instead of reading the first line.... (1 Reply)
Discussion started by: talkhard154
1 Replies

4. Web Development

Perl scripting or shell scripting?

i am going to study any one of the scripting languages mentioned above(shell 0r perl scripting) . Which is having more scope for a fresher? (1 Reply)
Discussion started by: Anna Hussie
1 Replies

5. What is on Your Mind?

Shell Scripting vs Perl scripting

Gents, I have been working in a Solaris/Unix environment for about 9 months. I took some linux classses online before getting the job. But, I am not very good at scripting. I want to learn how to script. Do you think that I should start with Shell scripting or Perl? I wanted to continue with... (2 Replies)
Discussion started by: Pouchie1
2 Replies

6. Shell Programming and Scripting

perl (homework)

------------------------------------------------------------------ -----------------------------------------------------------------? (1 Reply)
Discussion started by: Ame
1 Replies

7. What is on Your Mind?

Shell scripting vs Perl scripting

Hi all, I would like to start developping some good scripting skills. Do you think it would be best to start with shell scripting or Perl? I already got a fundation, really basics, in perl. but I am wondering what would be best to be good at first. Can you please help me determine which one to... (14 Replies)
Discussion started by: Pouchie1
14 Replies

8. Shell Programming and Scripting

Call Shell scripting from Perl Scripting.

Hi How to call a shell scripting through a Perl scripting? Actually I need some value from Shell scripting and passes in the Perl scripting. So how can i do this? (2 Replies)
Discussion started by: anupdas
2 Replies

9. Shell Programming and Scripting

Unix Scripting Question (Homework)

Hello im currently taking up Operating Systems and we're on unix ryt now. we have to make a script and here are the requirements.. can someone please help me. I would reli appreciate it.. because i dont reli know how to make scripts yet. Name of script : parkingcharge.sh Create a script that... (1 Reply)
Discussion started by: pitdark
1 Replies

10. Shell Programming and Scripting

Need Help with Perl Scripting Issue.

The Perl script runs fine without any error from command line. The script executes an external command and prints the output on a web page. But somehow when I call it from URL it does not work. Any help will be appreciated. Here is the script: --test.pl #!/usr/bin/perl # use warnings; use... (23 Replies)
Discussion started by: manik112
23 Replies
Login or Register to Ask a Question