I don't get any output: I'm using at the 1st line within the script #!/usr/bin/perl -w Which has included the below script. To execute from command line I input "This scripts name" add 2 4 <ret> Still get a syntax error:(
Juan Amore HPCS ENGINEERING 1-748-8789 -----Original Message----- From: Peter Scott [mailto:[EMAIL PROTECTED]] Sent: Wednesday, October 17, 2001 4:55 PM To: AMORE,JUAN (HP-Roseville,ex1); 'Gibbs Tanton - tgibbs'; AMORE,JUAN (HP-Roseville,ex1); 'Beginners@Perl. Org (E-mail)' Subject: RE: Anyone know to how use subroutines to add or multiply numbers :How would I modify my file: At 04:28 PM 10/17/01 -0700, AMORE,JUAN (HP-Roseville,ex1) wrote: >Hello Tanton or Perl Gurus, >I attempt to run this file but I get a syntax error in the location of >"foreach (@list)..? What is your output from /usr/bin/perl -v ? I'll bet it says 5.004 or earlier. >Can someone advise if I do have a syntax error, I tried many ways of >adjusting that area but no luck:( >Please Help!! > >#!/usr/bin/perl -w >my $function = shift @ARGV; > >if( $function =~ /add/ ) { > $rtn = add( @ARGV ); > print "The sum is :$rtn"; >} >elsif( $function =~ /multiply/ ) { > $rtn = multiply( @ARGV ); > print "The product is :$rtn"; >} > >sub add { > my @list = @_; > my $sum = 0; > $sum += $_ foreach (@list); > >sub multiply { > my @list = @_; > my $prod = 1; > $prod *= $_ foreach (@list); >} > >"file" 35 lines, 795 characters >prompt> file add 2 2 <ret> # this is the command line." > >syntax error at file line 27, near "$_ foreach " >syntax error at file line 33, near "$_ foreach " >Execution of file aborted due to compilation errors. > >Juan Amore HPCS ENGINEERING 1-748-8789 > > >-----Original Message----- >From: Gibbs Tanton - tgibbs [mailto:[EMAIL PROTECTED]] >Sent: Wednesday, October 17, 2001 1:38 PM >To: 'AMORE,JUAN (HP-Roseville,ex1)'; Beginners@Perl. Org (E-mail) >Subject: RE: Anyone know to how use subroutines to add or multiply >numbers :How would I modify my file: > > >You probably want to do something like: > >my $function = shift @ARGV; # function is now "add" or whatever and @ARGV >has one less > # argument in it (e.g. $ARGV[0] == 1 && $ARGV[1] >== 2) > >if( $function =~ /add/ ) { > $rtn = add( @ARGV ); # pass the remaining arguments to the function >} >elsif( $function =~ /multiply/ ) { > $rtn = multiply( @ARGV ); >} > >sub add { > my @list = @_; # make list be the arguments passed in to add > my $sum = 0; > $sum += $_ foreach( @list ); # loop through each element and add it to sum >} > >sub multiply { > my @list = @_; > my $prod = 1; > $prod *= $_ foreach( @list ); >} > >Hope this helps. > >Tanton >-----Original Message----- >From: AMORE,JUAN (HP-Roseville,ex1) [mailto:[EMAIL PROTECTED]] >Sent: Wednesday, October 17, 2001 3:31 PM >To: Beginners@Perl. Org (E-mail) >Cc: AMORE,JUAN (HP-Roseville,ex1) >Subject: FW: Anyone know to how use subroutines to add or multiply >numbers :How would I modify my file: > > > >Hello Perl gurus; >Can anyone help me show what to add in the sub routine area using a >length array $len=@ARGV so that I can add or multiply numbers. >Command line is "This file" add 1 2 <ret> > >I trying to use >if ($ARGV[0]=~/add/){ > $rtn=add(...); > print "The sum is:$rtn"; >} >elsif ($ARGV[0]=~/multiply/){ > $rtn=multiply(...); > print "The product is:$rtn"; >} > >sub add { > >} > >sub mulitply { > >} > >Thanks:) > >-- >To unsubscribe, e-mail: [EMAIL PROTECTED] >For additional commands, e-mail: [EMAIL PROTECTED] > >-- >To unsubscribe, e-mail: [EMAIL PROTECTED] >For additional commands, e-mail: [EMAIL PROTECTED] > >-- >To unsubscribe, e-mail: [EMAIL PROTECTED] >For additional commands, e-mail: [EMAIL PROTECTED] -- Peter Scott Pacific Systems Design Technologies http://www.perldebugged.com -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]