Re: need help with subroutines & funtion

This is a discussion on Re: need help with subroutines & funtion within the Perl forums in Programming Languages category; 2008/9/8 Raul Ruiz Jr. <fast.linux @ 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? Hello, Please always 'use strict' at your scripts. This can work: # cat t1.pl use strict; require 'lib.pl'; my @userArray = <STDIN>; my $sum = sumIt(@userArray); print $sum; # cat lib.pl use strict; sub sumIt(){ my @functionArray = @_; my $functionSum; foreach my $line (@functionArray){ $functionSum += $line; ...

Go Back   Application Development Forum > Programming Languages > Perl

Object Mix

Register FAQ Calendar Search Today's Posts Mark Forums Read
  #1  
Old 09-08-2008, 06:17 AM
Jeff Pang
Guest
 
Default Re: need help with subroutines & funtion

2008/9/8 Raul Ruiz Jr. <fast.linux@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?



Hello,

Please always 'use strict' at your scripts.
This can work:

# cat t1.pl
use strict;
require 'lib.pl';

my @userArray = <STDIN>;

my $sum = sumIt(@userArray);

print $sum;


# cat lib.pl
use strict;

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

1;


# perl t1.pl
1
2
3
4
(ctrl + D)

The result is 10.

The script t1.pl above reads arguments from STDIN, if you want to read
from command line, then change the statement to:

my @userArray = @ARGV;

Good luck.
Reply With Quote
Reply


Thread Tools
Display Modes


All times are GMT -5. The time now is 10:15 AM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
vB Ad Management by =RedTyger=

In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.