From fast.linux at yahoo.com Mon Sep 8 03:04:33 2008 From: fast.linux at yahoo.com (Raul Ruiz Jr.) Date: Mon, 8 Sep 2008 03:04:33 -0700 (PDT) Subject: [Oc-pm] need help with subroutines & funtions Message-ID: <310665.43990.qm@web45706.mail.sp1.yahoo.com> 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 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, 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 = ; $sum = sumIt(@userArray); print $sum; #_END_ ? And then for?my library: sub sumIt(){ ?? @functionArray = @_; ?? foreach $line(@functionArray){ ?? $functionSum += $line; ?? } ?? return $functionSum; } 1; -------------- next part -------------- An HTML attachment was scrubbed... URL: From btilly at gmail.com Mon Sep 8 08:27:19 2008 From: btilly at gmail.com (Ben Tilly) Date: Mon, 8 Sep 2008 08:27:19 -0700 Subject: [Oc-pm] need help with subroutines & funtion In-Reply-To: <310665.43990.qm@web45706.mail.sp1.yahoo.com> References: <310665.43990.qm@web45706.mail.sp1.yahoo.com> Message-ID: On Mon, Sep 8, 2008 at 3:04 AM, 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? > 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 From fast.linux at yahoo.com Tue Sep 16 22:20:49 2008 From: fast.linux at yahoo.com (Raul Ruiz Jr.) Date: Tue, 16 Sep 2008 22:20:49 -0700 (PDT) Subject: [Oc-pm] illegal division by zero? Message-ID: <517074.18157.qm@web45703.mail.sp1.yahoo.com> I have a question? My instructor did not pass me on my script/library project because of "illegal division by zero on line 14 of obj13-lib.pl (my library) part of my script. What am I doing wrong and can someone help me? I would really appreciate help with this. ?I thought I got this correct. This is a description of my project goal, and below is my graded script.: Write a script called obj13-1.pl and a library called obj13-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 total of all of the numbers added together, and a new array of numbers which is comprised of the other numbers divided by 2. Objective: See Project Page. Here are the contents of the file you handed in: Script output: ------------------------------- Illegal division by zero at obj13-lib.pl line 14. ------------------------------- Script obj13.pl contents: ------------------------------- #!/usr/bin/perl require 'obj13-lib.pl'; while() { chomp; last if ! /\d/; push(@userArray,$_); } ($sum,$avg) = &sumIt(@userArray); print "Total:$sum\nAverage:$avg\n"; #_END_ Overall Comments: Good! What about the new array that is created using the original array elements, divided by 2? My library script obj13-lib.pl below: #!/usr/bin/perl sub sumIt { @functionArray = @_; foreach $line(@functionArray){ $functionSum += $line; $count++ ?????? } return ($functionSum,$functionSum / $count); } 1; #_END_ -------------- next part -------------- An HTML attachment was scrubbed... URL: From chris at chrisgrau.com Wed Sep 17 08:42:14 2008 From: chris at chrisgrau.com (Chris Grau) Date: Wed, 17 Sep 2008 08:42:14 -0700 Subject: [Oc-pm] illegal division by zero? In-Reply-To: <517074.18157.qm@web45703.mail.sp1.yahoo.com> References: <517074.18157.qm@web45703.mail.sp1.yahoo.com> Message-ID: <20080917154214.GM24974@chrisgrau.com> On Tue, Sep 16, 2008 at 10:20:49PM -0700, Raul Ruiz Jr. wrote: > I have a question? My instructor did not pass me on my script/library > project because of "illegal division by zero on line 14 of > obj13-lib.pl (my library) part of my script. > What am I doing wrong and can someone help me? [snip] > Write a script called obj13-1.pl and a library called obj13-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 total of all of the numbers added > together, and a new array of numbers which is comprised of the other > numbers divided by 2. I had to reformat the function, because the indentation was painful to me. > sub sumIt { > @functionArray = @_; > foreach $line(@functionArray){ > $functionSum += $line; > $count++ > } > return ($functionSum,$functionSum / $count); > } The lack of use strict/variable declarations disturbs me, first of all. What happens when sumIt() is called a second time? More to the point at hand, what happens when sumIt() is called with an empty list as an argument? Also, you're missing something. The problem description lists three things that are to be returned. This sumIt() function only returns two. -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 189 bytes Desc: not available URL: From chris at chrisgrau.com Wed Sep 24 13:43:53 2008 From: chris at chrisgrau.com (Chris Grau) Date: Wed, 24 Sep 2008 13:43:53 -0700 Subject: [Oc-pm] [San-Diego-pm] Damian Conway is coming... Message-ID: <20080924204352.GD26177@chrisgrau.com> ----- Forwarded message from Bob Kleemann ----- From: Bob Kleemann To: Perl Mongers Subject: [San-Diego-pm] Damian Conway is coming... Date: Tue, 23 Sep 2008 23:36:53 -0700 Ladies and Gentlemen, Techies and Programmers, The San Diego Perl Mongers are happy to announce, for one night only, the one and only Damian Conway! He will be appearing Monday night, September 29 at the Qualcomm Building Q Auditorium. Please arrive by 7 PM to hear him muse on a myriad of topics, including, but not limited to (in his own words): modern archaeological techniques, bidirectional cross- dressing, Ancient Greeks hackers, improbable romances, the real Club Med, why programmers shouldn't frequent casinos, the language of moisture vaporators, C++ mysticism, conversational Latin, state machines on steroids, feeding the dog the old-fashioned way, the shocking truth about anime, programming without variables or subroutines, the Four Voids of the Apocalypse, Microsoft's new advertising campaign, what the Romans used instead of braces, drunken stonemasons, the ancient probabilistic wisdom of bodkins, how to kill a language with a single byte, and the price of fish. Note: Topics are subject to change without notice, reason, or warning, so please pay attention! So please come on by and learn something new. Bring some friends if you like, and have fun in the process. Please let me know if there are any questions. _______________________________________________ San-Diego-pm mailing list San-Diego-pm at pm.org http://mail.pm.org/mailman/listinfo/san-diego-pm ----- End forwarded message ----- -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 189 bytes Desc: not available URL: From peter.t.wilson at gmail.com Sat Sep 27 13:58:35 2008 From: peter.t.wilson at gmail.com (Pete Wilson) Date: Sat, 27 Sep 2008 13:58:35 -0700 Subject: [Oc-pm] [San-Diego-pm] Damian Conway is coming... In-Reply-To: <20080924204352.GD26177@chrisgrau.com> References: <20080924204352.GD26177@chrisgrau.com> Message-ID: <4c3ca8c50809271358g37f994c4yffc6d543971eed42@mail.gmail.com> Is anyone else from OC-PM thinking of going? I will probably go. Does anyone want to carpool? -Pete On Wed, Sep 24, 2008 at 1:43 PM, Chris Grau wrote: > ----- Forwarded message from Bob Kleemann ----- > > From: Bob Kleemann > To: Perl Mongers > Subject: [San-Diego-pm] Damian Conway is coming... > Date: Tue, 23 Sep 2008 23:36:53 -0700 > > Ladies and Gentlemen, Techies and Programmers, > > The San Diego Perl Mongers are happy to announce, for one night only, the > one > and only Damian Conway! He will be appearing Monday night, September 29 at > the Qualcomm Building Q Auditorium. Please arrive by 7 PM to hear him muse > on a myriad of topics, including, but not limited to (in his own words): > > modern archaeological techniques, bidirectional cross- dressing, Ancient > Greeks hackers, improbable romances, the real Club Med, why programmers > shouldn't frequent casinos, the language of moisture vaporators, C++ > mysticism, conversational Latin, state machines on steroids, feeding the > dog the old-fashioned way, the shocking truth about anime, programming > without variables or subroutines, the Four Voids of the Apocalypse, > Microsoft's new advertising campaign, what the Romans used instead of > braces, drunken stonemasons, the ancient probabilistic wisdom of > bodkins, > how to kill a language with a single byte, and the price of fish. > > Note: Topics are subject to change without notice, reason, or warning, so > please pay attention! > > So please come on by and learn something new. Bring some friends if you > like, > and have fun in the process. > > Please let me know if there are any questions. > > _______________________________________________ > San-Diego-pm mailing list > San-Diego-pm at pm.org > http://mail.pm.org/mailman/listinfo/san-diego-pm > > ----- End forwarded message ----- > > _______________________________________________ > Oc-pm mailing list > Oc-pm at pm.org > http://mail.pm.org/mailman/listinfo/oc-pm > -------------- next part -------------- An HTML attachment was scrubbed... URL: