| Register | FAQ | Calendar | Search | Today's Posts | Mark Forums Read |
|
#1
| |||
| |||
| I am taking an online ceu course in scripting with Unix. I have been stumped by this project. Can anyone out there help me out a bit. I created a script at the bottom and it does not quite work. What am I missing?* I know I am missing some thing but this is my first time working with perl.I know it will take me a while to get good at it but so far its been fun.* I would appreciate the help if possible. Here is my project and below is my code. * Write a script called obj.pl and a library called obj-lib.pl. The library should contain a function that takes in an array of numbers (of arbitrary size). The function will then calculate the average of the numbers, the totalof 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 listwith all of that information. The script file, obj.pl should get a list of numbers from the user (either via STDIN or a list of arguments) and call the library function. * Here is my script to solve the project. * #!/usr/bin/perl require 'lib.pl'; @userArray = <STDIN>; $sum = sumIt(@userArray); print $sum; #_END_ * And then for*my library: sub sumIt(){ ** @functionArray = @_; ** foreach $line(@functionArray){ ** $functionSum += $line; ** } ** return $functionSum; } 1; |
|
#2
| |||
| |||
| Raul Ruiz Jr. wrote: > I am taking an online ceu course in scripting with Unix. I have been > stumped by this project. Can anyone out there help me out a bit. I > created a script at the bottom and it does not quite work. What am I > missing? perldoc -q "How do I do .anything.?" > I know I am missing some thing but this is my first time working with > perl. I know it will take me a while to get good at it but so far its > been fun. I would appreciate the help if possible. > Here is my project and below is my code. > > Write a script called obj.pl and a library called obj-lib.pl. The > library should contain a function that takes in an array of numbers > (of arbitrary size). perldoc perlsub > 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. So you have to return *three* things. > The script file, obj.pl should get a list of numbers from the user > (either via STDIN or a list of arguments) and call the library The list of arguments is stored in the @ARGV array. perldoc perlvar > function. > > Here is my script to solve the project. > > #!/usr/bin/perl You should probably start your program with the warnings and strict pragmas to help you find common mistakes. use warnings; use strict; > require 'lib.pl'; > > @userArray = <STDIN>; > > $sum = sumIt(@userArray); You are supposed to return *three* things. > print $sum; > > #_END_ > > And then for my library: > > sub sumIt(){ Your prototype says that sumIt does not accept any arguments. You shouldn't use prototypes. perldoc perlsub > @functionArray = @_; > foreach $line(@functionArray){ > $functionSum += $line; > } > return $functionSum; Where is "the average of the numbers" and "a new array of numbers which is comprised of the other numbers divided by 2"? > } > > 1; John -- Perl isn't a toolbox, but a small machine shop where you can special-order certain sorts of tools at low cost and in short order. -- Larry Wall |
|
#3
| |||
| |||
| On Mon, Sep 8, 2008 at 3:04 AM, Raul Ruiz Jr. <fast.linux@yahoo.com> wrote: > I am taking an online ceu course in scripting with Unix. I have been stumped > by this project. Can anyone out there help me out a bit. I created a script > at the bottom and it does not quite work. What am I missing? > I know I am missing some thing but this is my first time working with perl. > I know it will take me a while to get good at it but so far its been fun. I > would appreciate the help if possible. I won't give you the full answer, but I'll give you a couple of tips. The first is that good Perl practice is to _always_ put the line: use strict; at the top of every file, and then declare variables when you first use them. Usually with "my". You can also declare globals with use vars or our. The point of that is to catch many typos automatically. (It catches a couple of other important mistakes as well, but typo catching is the main win.) Of course that introduces scoping. An old, but still good, introduction to the complexities of scoping in Perl is http://perl.plover.com/FAQs/Namespaces.html. That said, here is the problem that probably has you stumped. In your library you have: > sub sumIt(){ > @functionArray = @_; > foreach $line(@functionArray){ > $functionSum += $line; > } > return $functionSum; > } Unfortunately the pair of parentheses in the function declaration is a prototype telling Perl that this function takes no arguments. Don't worry about what prototypes are for, just know that prototypes are almost always the wrong thing to do. So change that declaration to: sub sumIt { ... } And incorporating my scoping advice that code would become: sub sumIt { my @functionArray = @_; my $functionSum = 0; foreach my $line (@functionArray) { $functionSum += $line; } return $functionSum; } Cheers, Ben |
![]() |
| Thread Tools | |
| Display Modes | |
In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.