This is my script.
#!/usr/bin/perl
#require 'lib.pl';
@userArray = <STDIN>;
$sum = sumIt(@userArray);
print $sum;
And this is my library according to yitzle:
sub sumIt(@)
{
my $total = 0;
$total += $_ for (@_);
return $total; # This line might not be needed...
}
sub avg(@)
{
my @arr = @_;
my $arrSize = @arr; # scalar(@arr) is the array size - or one less
(last index). Double check
return simIt(@arr) / $arrSize;
}
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
Or is this nonsensical? Very very new to Perl.
Thanks
Amichai
Ack prototypes! They don't do what you think they do. Don't use them
until you have read
http://library.n0i.net/programming/perl/articles/fm_prototypes and
understand it fully. Prototypes are very useful in specific areas and
harmful or a waste of your time in all others. Perl 6 is adding
formal parameters (which is what people mistake prototypes for), so
this will not be a problem in it.
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
require 'lib.pl';
>
> @userArray = <STDIN>;
>
> $sum = sumIt(@userArray);
>
> print $sum;
If the sub sumIt() is in the file lib.pl, you have to tell perl where to find it.
This is pure educational. I want to understand how this all works.
So after follow your comments my script.pl looks like this:
!/usr/bin/perl
use strict;
use warnings;
require 'lib.pl';
my @userArray = <STDIN>;
my $sum = sumIt(@userArray);
print $sum;
AND my library like this:
sub sumIt(
my $total;
$total += $_ for @_;
warn "@_ was empty, total undefined!\n" if !defined $total;
}
sub avg(@)
{
my @arr = @_;
my $arrSize = scalar(@arr);
(last index). Double check
return sumIt(@arr) / @arr;
}
1;
Is this correct now? If so, I don't know how to use it. What command should
I use to try this out?
Thanks
Amichai
P.S. echo 1234 | ./script.pl doesn't do anythign exciting.
> --
> To unsubscribe, e-mail: beginners-...@perl.org
> For additional commands, e-mail: beginne...@perl.org
> http://learn.perl.org/
>
>
>
This shebang is incorrect. Specifically, you're missing the "sh" part
of "shebang":
#!/usr/bin/perl
> use strict;
> use warnings;
>
> require 'lib.pl';
>
> my @userArray = <STDIN>;
>
> my $sum = sumIt(@userArray);
>
> print $sum;
>
> AND my library like this:
>
> sub sumIt(
A parenthesis does not begin a subroutine. A curly-brace does.
> my $total;
> $total += $_ for @_;
> warn "@_ was empty, total undefined!\n" if !defined $total;
> }
You misread my post. I said this is what you very specifically DO NOT
want to do. I said this sort of thing is exactly why you should
ALWAYS use an explicit return statement.
sub sumIt {
my $total;
$total += $_ for @_;
warn "\@_ was empty, total undefined!\n" if !defined $total;
return $total;
}
>
> sub avg(@)
you are still using prototypes, after being told why not to.
> {
> my @arr = @_;
> my $arrSize = scalar(@arr);
Now that you've elimintated the $arrSize variable from the division
below, there is no reason to create it in the first place.
> (last index). Double check
This is a syntax error, as you haven't preceded your comment with a #
mark
> return sumIt(@arr) / @arr;
>
> }
>
> 1;
>
> Is this correct now?
No. See above.
> If so, I don't know how to use it. What command should
> I use to try this out?
Is this your first Perl script ever? If so, you really need to be
reading
perldoc perlintro
long before worrying about subroutines and prototypes and return
values and creating libraries.
If not, you run it the same way you run any other Perl script.
Either
perl script.pl
or make script.pl executable:
chmod u+x script.pl
and then run it:
./script.pl
> P.S. echo 1234 | ./script.pl doesn't do anythign exciting.
Yes it does. It gives you all the syntax errors you had in your code.
Paul Lalli
> > require 'lib.pl';
> >> @userArray = <STDIN>;
> >> $sum = sumIt(@userArray);
> >> print $sum;
>
> > If the sub sumIt() is in the file lib.pl, you have to tell perl where
> > to find it.
> I thought that
> require 'lib.pl';
> was telling Perl where to find it.
Yes, it would have, if that's what you had. But you didn't. You had
the line:
#require 'lib.pl'
That is, you had the line commented out. A commented out line does
nothing at all.
Paul Lalli
Actually, in the post I replied to it was: #require 'script.pl';
The OP seems to think that we are keeping a copy of his program and whenever he changes something, our copy get automatically updated. Sorry, computers just don't work that way.
No, in the post you replied to, it was 'lib.pl':
http://groups.google.com/group/perl.beginners/msg/61455df84cce63c9
In your reply to said post, you changed it to look like he had
'script.pl':
http://groups.google.com/group/perl.beginners/msg/0bd8e1be8c6b0cdf
I changed it back in my followup.
Paul Lalli
What the heck is obj13-1.pl?
To test your script you need a list of numbers one per line. The easiest way is:
$ perl script.pl
123
456
789
^D
The '^D' is generated by holding the Control (sometimes labeled Ctrl) key and pressing the d key at the same time.
Okay. I'm an ass. Google Groups is having (yet more) problems today,
and one of them resulted in me seeing your reply that had 'script.pl'
before Amichai's post that had 'script.pl', instead showing your reply
as being in response to his ORIGINAL post that had 'lib.pl'.
Consequently, I assumed you had changed his text in your quoted reply.
Now Amichai's post that contains 'script.pl' has indeed shown up, and
I look and feel like a completely jerk, and rightly so.
Though my initial response to this has also not yet shown up on Google
Groups, it basically calls you a liar (though in not so many words).
I very much profusley and humbly apologize for that reaction. I was
100% in the wrong, placing my faith in a service I know to be buggy as
all hell.
Sincerely,
Paul Lalli
echo 1234 | ./script.pl obviously won't do much. I wad told
./obj13-1.pl <<EOD
Yet that doesn't seem to work either.
Mr. Shawn H. Corey wrote:
So it looks now like this:
#!/usr/bin/perl
use strict;
use warnings;
require 'obj13-lib.pl';
my @userArray = <STDIN>;
my $sum = sumIt(@userArray);
print $sum;
and the library I renamed to: obj13-lib.pl and now looks like this:
sub sumIt{
my $total;
$total += $_ for @_;
warn "@_ was empty, total undefined!\n" if !defined $total;
}
sub avg(@)
{
my @arr = @_;
my $arrSize = scalar(@arr);
(last index). Double check
return sumIt(@arr) / @arr;
}
1;
When I do the following:
:~$ perl obj13-1.pl
I get:
Not enough arguments for index at obj13-lib.pl line 11, near "index)"
Compilation failed in require at obj13-1.pl line 6.
What is this index error?
Mr. Shawn H. Corey wrote:
This will return nothing useful. You need a return statement after the warn.
> sub avg(@)
> {
> my @arr = @_;
> my $arrSize = scalar(@arr);
> (last index). Double check
> return sumIt(@arr) / @arr;
> }
snip
> Not enough arguments for index at obj13-lib.pl line 11, near "index)"
> Compilation failed in require at obj13-1.pl line 6.
>
> What is this index error?
snip
You are getting that error because what used to be a comment is now
(last index). Double check
Next time you get an error message look at the line (in this case 11)
and see if there is anything obviously wrong with it. If there isn't
anything wrong with that line the problem is almost always above it.
Go to line 11 in obj13-lib.pl and check its syntax.
sub sumIt{
my $total;
$total += $_ for @_;
warn "@_ was empty, total undefined!\n" if !defined $total;
}
sub avg(@)
{
my @arr = @_;
my $arrSize = scalar(@arr);
#(last index). Double check;
return sumIt(@arr) / @arr;
}
1;
Why is
return sumIt(@arr) / @arr;
wrong syntax?
It was suggested that I have to call the sumIt function.
(last index). Double check
shouldn't even be there. I must have incompletely removed the comment. So I should just delete it or comment it out?
(last index). Double check
I don't know what could be wrong with this syntax, as I have never used
these commands before. I thought it migh need a curly, but I get the
same error.
Mr. Shawn H. Corey wrote:
> Amichai Teumim wrote:
>> I get:
>>
>> Not enough arguments for index at obj13-lib.pl line 11, near "index)"
>> Compilation failed in require at obj13-1.pl line 6.
>>
>> What is this index error?
>
-----Original Message-----
>From: Amichai Teumim <amichai...@idtglobal.com>
>Sent: Aug 6, 2007 10:34 AM
>To: "Mr. Shawn H. Corey" <shawn...@magma.ca>
>Cc: begi...@perl.org
>Subject: Re: library (random numbers)
>
>I just renamed it to obj13-1.pl from script.pl
>
>So it looks now like this:
>
>#!/usr/bin/perl
>
>use strict;
>use warnings;
>
>require 'obj13-lib.pl';
>
>my @userArray = <STDIN>;
>
>my $sum = sumIt(@userArray);
>
>print $sum;
>
>and the library I renamed to: obj13-lib.pl and now looks like this:
>
>
>sub sumIt{
> my $total;
> $total += $_ for @_;
> warn "@_ was empty, total undefined!\n" if !defined $total;
>}
>
>sub avg(@)
>{
> my @arr = @_;
> my $arrSize = scalar(@arr);
>(last index). Double check
I don't know what's this line.Maybe it's a comment?
--
Jeff Pang <pa...@earthlink.net>
http://home.arcor.de/jeffpang/
it is a comment
you should delete it or
write a # in front of it.
Here is the script again:
#!/usr/bin/perl
use strict;
use warnings;
require 'obj13-lib.pl';
my @userArray = <STDIN>;
my $sum = sumIt(@userArray);
print $sum;
And here is the library:
sub sumIt{
my $total;
$total += $_ for @_;
warn "@_ was empty, total undefined!\n" if !defined $total;
}
sub avg(@)
{
my @arr = @_;
my $arrSize = scalar(@arr);
#(last index). Double check;
my $sum = sumIt(@userArray);
return sumIt(@arr) / @sum;
}
1;
Does the library make sense?