On Aug 3, 6:03 am, [EMAIL PROTECTED] (Amichai Teumim) wrote: > After some friendly input from yitzle I might have moved further with > my library. > > This is my script. > > script.pl > > #!/usr/bin/perl
Get into the habbit of using use strict; use warnings; in all of your scripts. You will be greatful you did in the long run. With such small libraries/scripts as these, it's not as important as mistakes are far easier to debug, but getting into the habbit now will save you endless amounts of pain later on in your programming life. > #require 'lib.pl'; > > @userArray = <STDIN>; my @userArray = <STDIN>; because 'use strict;' forces you to declare your variables. > $sum = sumIt(@userArray); my $sum = sumIt(@userArray); Out of curiousity, is this a learning excercise, or do you intend to actually use this? If the latter, you should know that this wheel has already been invented. The standard List::Util module provides a sum() function: use List::Util qw/sum/; my $sum = sum(@userArray); > print $sum; > > And this is my library according to yitzle: > > sub sumIt(@) 1) Don't use prototypes. They don't work like anyone expects them to. 2) This particular prototype is double-plus useless. It says that this function takes a list of values. That's what all subroutines without prototypes take. That (@) is doing nothing at all. > { > my $total = 0; > $total += $_ for (@_); > return $total; # This line might not be needed... Not needed, but again a good habbit to get into. Never rely on the "blocks return the last evaluated value" feature of Perl. Return explicitly, so that you don't FUBAR things when you later go back to modify your code. For example, if you had just written: sub sumIt { my $total; $total += $_ for @_; } and then later you wanted to add a warning if there hadn't been anything in @_: sub sumIt { my $total; $total += $_ for @_; warn "@_ was empty, total undefined!!\n" if !defined $total; } Now you've FUBARed your script. The last value evaluated will either be the (!defined $total), or the (warn "..."). It won't be $total. If you'd started with the explicit return statement, you'd save yourself this problem. > > } > > sub avg(@) > { > my @arr = @_; > my $arrSize = @arr; # scalar(@arr) is the array size - or one less > (last index). Double check scalar(@arr) is the size of the array $#arr is the last index of the array. USUALLY it is a true statement that scalar(@arr) == $#arr + 1; However, this is not necessarily the case, as in Perl you can actually futz with the starting index of arrays using the $[ variable. You should never do that, of course, but you also shouldn't assume that no one in your program has. > return simIt(@arr) / $arrSize; Well first, assuming you meant sumIt, not simit, there's no reason to create a new variable just to store the size of the array. Just use the array in a scalar context: return sumIt(@arr) / @arr; > > } > > 1; > > Now either this is wrong or I have no idea how to use it. > > With STDIN I need to do something like this (right?): > > ./script.pl | echo 1234 That says you want to run script.pl and send the output of script.pl to the process "echo 1234". You actually want the other way around: echo 1234 | ./script.pl > Or is this nonsensical? Very very new to Perl. Well, this bit in any event has nothing to do with Perl. Input/Output redirection is a feature of the shell, not of Perl. Paul Lalli -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/