Re: library (random numbers)

2007-08-07 Thread A. Teumim - Netgear Support
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 [EMAIL PROTECTED] 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?

 Go to line 11 in obj13-lib.pl and check its syntax.


 --
 Just my 0.0002 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: [EMAIL PROTECTED]
 For additional commands, e-mail: [EMAIL PROTECTED]
 http://learn.perl.org/





Re: library (random numbers)

2007-08-07 Thread Amichai Teumim
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 [EMAIL PROTECTED] 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.
   

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




library (random numbers)

2007-08-07 Thread Amichai Teumim

Line 11 n obj13-lib.pl is:

(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?

 Go to line 11 in obj13-lib.pl and check its syntax.




-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: library (random numbers)

2007-08-07 Thread Jeff Pang


-Original Message-
From: Amichai Teumim [EMAIL PROTECTED]
Sent: Aug 6, 2007 10:34 AM
To: Mr. Shawn H. Corey [EMAIL PROTECTED]
Cc: beginners@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 [EMAIL PROTECTED]
http://home.arcor.de/jeffpang/

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: library (random numbers)

2007-08-07 Thread Martin Barth
On Tue, 07 Aug 2007 11:23:05 +0300
Amichai Teumim [EMAIL PROTECTED] wrote:

 
 Line 11 n obj13-lib.pl is:
 
 (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?
 
  Go to line 11 in obj13-lib.pl and check its syntax.
 
 
 
 

it is a comment
you should delete it or
write a # in front of it.

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: library (random numbers)

2007-08-07 Thread Amichai Teumim
Yeah I think it was a comment. I removed it now. Thanks ;)

Jeff Pang wrote:
 -Original Message-
   
 From: Amichai Teumim [EMAIL PROTECTED]
 Sent: Aug 6, 2007 10:34 AM
 To: Mr. Shawn H. Corey [EMAIL PROTECTED]
 Cc: beginners@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 [EMAIL PROTECTED]
 http://home.arcor.de/jeffpang/

   


library (random numbers)

2007-08-07 Thread Amichai Teumim
Does this make any sense?

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?

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: library (random numbers)

2007-08-06 Thread Mr. Shawn H. Corey

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.0002 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: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: library (random numbers)

2007-08-06 Thread Amichai Teumim
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 [EMAIL PROTECTED] wrote:

 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/





Re: library (random numbers)

2007-08-06 Thread Amichai Teumim
I thought that

require 'lib.pl';

was telling Perl where to find it.

Mr. Shawn H. Corey wrote:
 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.



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: library (random numbers)

2007-08-06 Thread Paul Lalli
On Aug 6, 6:52 am, [EMAIL PROTECTED] (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 [EMAIL PROTECTED] 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


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: library (random numbers)

2007-08-06 Thread Paul Lalli
On Aug 6, 7:56 am, [EMAIL PROTECTED] (Amichai Teumim)
wrote:
 Mr. Shawn H. Corey wrote:
  Amichai Teumim wrote:
  #!/usr/bin/perl
  #require 'lib.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.

 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


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: library (random numbers)

2007-08-06 Thread Mr. Shawn H. Corey

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.0002 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: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: library (random numbers)

2007-08-06 Thread Amichai Teumim
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:
 ./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



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: library (random numbers)

2007-08-06 Thread Mr. Shawn H. Corey

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.0002 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: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: library (random numbers)

2007-08-06 Thread Paul Lalli
On Aug 6, 9:03 am, [EMAIL PROTECTED] (Mr. Shawn H. Corey) wrote:
 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';

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


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: library (random numbers)

2007-08-06 Thread Paul Lalli
On Aug 6, 9:03 am, [EMAIL PROTECTED] (Mr. Shawn H. Corey) wrote:
 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';

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


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: library (random numbers)

2007-08-06 Thread Amichai Teumim
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:
 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.



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: library (random numbers)

2007-08-06 Thread Amichai Teumim
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:
 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.



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: library (random numbers)

2007-08-06 Thread Chas Owens
On 8/6/07, Amichai Teumim [EMAIL PROTECTED] 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.

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: library (random numbers)

2007-08-06 Thread Mr. Shawn H. Corey

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.0002 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: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: library (random numbers)

2007-08-03 Thread Mr. Shawn H. Corey

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.0002 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: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: library (random numbers)

2007-08-03 Thread Paul Lalli
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/




library (random numbers)

2007-08-03 Thread Amichai Teumim
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

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: library (random numbers)

2007-08-03 Thread Chas Owens
On 8/3/07, Amichai Teumim [EMAIL PROTECTED] 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/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.

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: library (random numberS)

2007-07-27 Thread Chas Owens
On 7/27/07, Amichai Teumim [EMAIL PROTECTED] wrote:
snip
 What do I add here to calculate the average of the numbers?
snip

Which average do you want (common ones are mean, mode, and median)?
Try looking them up on Wikipedia:

* http://en.wikipedia.org/wiki/Mean#Arithmetic_mean
* http://en.wikipedia.org/wiki/Median#Popular_explanation
* http://en.wikipedia.org/wiki/Mode_(statistics)

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




library (random numbers)

2007-07-27 Thread Amichai Teumim
OK I figured most of this out I think.

I want to create a library that contains a function which takes in an
array of numbers (random numbers). This function will then calculate the
average of these numbers, the total of all these numbers added together,
and a new array of numbers which is the other numbers divided by 2.
Then it should return a new list with that information.

Then I want to create a script which should get a list of number from
STDIN and call the library function.

I've come this far to realise that I should use this script. I'm really
new to Perl. And I'm trying to learn it big time, because I am switching
to Perl for my new System Admin job. If someone can provide me guidance
it would be greatly appreciated. I really need to understand how this works.

#!/usr/bin/perl

require 'lib.pl';

@userArray = STDIN;

$sum = sumIt(@userArray);

print $sum;

and then for the library:

sub sumIt(){
   @functionArray = @_;
   foreach $line(@functionArray){
   $functionSum += $line;
   }
   return $functionSum;
}

The top part would go in lets say script.pl and the second library part
would go into lib.pl. This far so good right?

Thanks

Amichai

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




library (random numberS)

2007-07-27 Thread Amichai Teumim
OK. I think I moved further.

So this is my library.

ub sumIt(){
   @functionArray = @_;
   foreach $line(@functionArray){
   $functionSum += $line;
   }
   return $functionSum;
}

1;

What do I add here to calculate the average of the numbers?

thanks

Amichai

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: library (random numberS)

2007-07-27 Thread yitzle
I was under the impression that average meant mean.

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;

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/




Re: library (random numberS)

2007-07-27 Thread Andy Greenwood
On 7/27/07, yitzle [EMAIL PROTECTED] wrote:
 I was under the impression that average meant mean.

correct. Average is the common term for mean. In fact, reading the
pages linked above, they do imply (without stating explicitly) that
the three terms describe different calculations.

I can't think of any situation where someone has said average and
meant mode or median.


 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;

 --
 To unsubscribe, e-mail: [EMAIL PROTECTED]
 For additional commands, e-mail: [EMAIL PROTECTED]
 http://learn.perl.org/





-- 
-- 
I'm nerdy in the extreme and whiter than sour cream

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/