Home > Archive > PERL Beginners > August 2007 > library (random numbers)
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
library (random numbers)
|
|
| Amichai Teumim 2007-08-03, 7:02 pm |
| After some friendly input from yitzle I might have moved further with
my library.
This is my script.
script.pl
#!/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
| |
| Mr. Shawn H. Corey 2007-08-03, 7:02 pm |
| Amichai Teumim wrote:
> ./script.pl | echo 1234
>
> Or is this nonsensical? Very very new to Perl.
This is actually shell:
echo 1234 | ./script.pl
Try:
../script.pl <<EOD
1234
5678
90
EOD
--
Just my 0.00000002 million dollars worth,
Shawn
"For the things we have to learn before we can do them, we learn by doing them."
Aristotle
| |
| Chas Owens 2007-08-03, 7:02 pm |
| On 8/3/07, Amichai Teumim <amichai.teumim@idtglobal.com> wrote:
snip
> sub sumIt(@)
snip
> sub avg(@)
snip
Ack prototypes! They don't do what you think they do. Don't use them
until you have read
http://library.n0i.net/programming/...s/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.
| |
| Paul Lalli 2007-08-03, 7:02 pm |
| On Aug 3, 6:03 am, amichai.teu...@idtglobal.com (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
| |
| Mr. Shawn H. Corey 2007-08-06, 7:58 am |
| Amichai Teumim wrote:
> When I do this, I get :
>
> ./script.pl <<EOD
> 1234
> 5678
> 90
> EOD
>
> Undefined subroutine &main::sumIt called at ./script.pl line 7, <STDIN>
> line 3.
>
> #!/usr/bin/perl
>
> #require 'script.pl';
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.
--
Just my 0.00000002 million dollars worth,
Shawn
"For the things we have to learn before we can do them, we learn by doing them."
Aristotle
| |
| Amichai Teumim 2007-08-06, 7:58 am |
| Hi Paul
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.
On 8/3/07, Paul Lalli <mritty@gmail.com> wrote:
>
> On Aug 3, 6:03 am, amichai.teu...@idtglobal.com (Amichai Teumim)
> wrote:
>
> 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.
>
>
>
> my @userArray = <STDIN>;
> because 'use strict;' forces you to declare your variables.
>
>
> 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);
>
>
> 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.
>
>
> 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.
>
>
> 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.
>
>
> 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;
>
>
> 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
>
>
> 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: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> http://learn.perl.org/
>
>
>
| |
| Amichai Teumim 2007-08-06, 7:58 am |
| I thought that
require 'lib.pl';
was telling Perl where to find it.
Mr. Shawn H. Corey wrote:
> Amichai Teumim wrote:
>
> require 'lib.pl';
>
>
> If the sub sumIt() is in the file lib.pl, you have to tell perl where
> to find it.
>
>
| |
| Paul Lalli 2007-08-06, 6:59 pm |
| On Aug 6, 6:52 am, amic...@teumim.com (Amichai Teumim) wrote:
> 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
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
| |
| Paul Lalli 2007-08-06, 6:59 pm |
| On Aug 6, 7:56 am, amichai.teu...@idtglobal.com (Amichai Teumim)
wrote:
> Mr. Shawn H. Corey wrote:
[color=darkred]
[color=darkred]
>
[color=darkred]
> 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
| |
| Mr. Shawn H. Corey 2007-08-06, 6:59 pm |
| Paul Lalli wrote:
> 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.
--
Just my 0.00000002 million dollars worth,
Shawn
"For the things we have to learn before we can do them, we learn by doing them."
Aristotle
| |
| Amichai Teumim 2007-08-06, 6:59 pm |
| When I do this, I get :
../script.pl <<EOD
1234
5678
90
EOD
Undefined subroutine &main::sumIt called at ./script.pl line 7, <STDIN>
line 3.
#!/usr/bin/perl
#require 'script.pl';
@userArray = <STDIN>;
$sum = sumIt(@userArray);
print $sum;
Mr. Shawn H. Corey wrote:
> Amichai Teumim wrote:
>
> This is actually shell:
>
> echo 1234 | ./script.pl
>
> Try:
>
> ./script.pl <<EOD
> 1234
> 5678
> 90
> EOD
>
>
| |
|
|
| Mr. Shawn H. Corey 2007-08-06, 6:59 pm |
| Amichai Teumim wrote:
> No it was commented out. Now it's changed. I still don't know how to use
> it. What would a good way to test it be?
>
> echo 1234 | ./script.pl obviously won't do much. I wad told
> ./obj13-1.pl <<EOD
>
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.
--
Just my 0.00000002 million dollars worth,
Shawn
"For the things we have to learn before we can do them, we learn by doing them."
Aristotle
| |
| Paul Lalli 2007-08-06, 6:59 pm |
| On Aug 6, 9:03 am, shawnhco...@magma.ca (Mr. Shawn H. Corey) wrote:
> Paul Lalli wrote:
>
>
>
> Actually, in the post I replied to it was: #require 'script.pl';
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
| |
|
| In-Reply-To: <46B71C2A.3080805@magma.ca>
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit
Approved: news@nntp.perl.org
From: amichai.teumim@idtglobal.com (Amichai Teumim)
Bytes: 2971
Lines: 30
Xref: number1.nntp.dca.giganews.com perl.beginners:49331
No it was commented out. Now it's changed. I still don't know how to use
it. What would a good way to test it be?
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:
> Paul Lalli wrote:
>
> 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.
>
>
| |
| Amichai Teumim 2007-08-06, 6:59 pm |
| 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
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:
> Amichai Teumim wrote:
>
> 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.
>
>
| |
| Chas Owens 2007-08-06, 6:59 pm |
| On 8/6/07, Amichai Teumim <amichai.teumim@idtglobal.com> wrote:
snip
> sub sumIt{
> my $total;
> $total += $_ for @_;
> warn "@_ was empty, total undefined!\n" if !defined $total;
> }
snip
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.
| |
| Mr. Shawn H. Corey 2007-08-06, 6:59 pm |
| 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?
Go to line 11 in obj13-lib.pl and check its syntax.
--
Just my 0.00000002 million dollars worth,
Shawn
"For the things we have to learn before we can do them, we learn by doing them."
Aristotle
| |
| A. Teumim - Netgear Support 2007-08-07, 4:01 am |
| So this is what I have now in my 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;
return sumIt(@arr) / @arr;
}
1;
Why is
return sumIt(@arr) / @arr;
wrong syntax?
It was suggested that I have to call the sumIt function.
On 8/6/07, Mr. Shawn H. Corey <shawnhcorey@magma.ca> wrote:
Amichai Teumim wrote:
>
> Go to line 11 in obj13-lib.pl and check its syntax.
>
>
> --
> Just my 0.00000002 million dollars worth,
> Shawn
>
> "For the things we have to learn before we can do them, we learn by doing
> them."
> Aristotle
>
> --
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org
> For additional commands, e-mail: beginners-help@perl.org
> http://learn.perl.org/
>
>
>
| |
| Amichai Teumim 2007-08-07, 7:59 am |
| So that makes sense.
(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?
Chas Owens wrote:
> On 8/6/07, Amichai Teumim <amichai.teumim@idtglobal.com> wrote:
> snip
>
> snip
>
> This will return nothing useful. You need a return statement after the warn.
>
>
> snip
>
> 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.
>
|
|
|
|
|