On Wed, Jul 16, 2003 at 07:50:56AM -0700, Stuart White wrote:
> I seem to be stuck on subroutines.
Okay, let's have a look;
> #################################
> # Subroutine prototypes
> ################################
>
> sub FoulParser($);
>
> #############################
> # Main Program
> #############################
>
> #!/usr/bin/perl
>
> use warnings;
> use strict;
>
> open(STATS, "stats.txt") or die "statfile\n";
> my $foul;
> while (<STATS>)
> {
> #if ($_ = /(\w+) (Foul:) (\w+)/)
> if ($_ =~ /Foul/)
> {
> $foul = "$_";
> FoulParser($foul);
> }
> }
This is beside the point, but:
open (STATS, "stats.txt")
or die "open: stats.txt: $!"; # better error message
while (<STATS>) {
next unless /Foul/; # use $_ to your advantage
FoulParser($_); # and pass it directly to the sub
}
>
>
> ############################
> # Subroutine Definitions
> ###########################
>
> #This subroutine takes a scalar $foul, that is a
> #parsed line from the file, a line that contains the
> #word "Foul" and it prints out that line.
>
>
> sub FoulParser($foul)
^^^^^
This doesn't do what you think it does. Perl doesn't (yet) have named
prototypes, and trying to use one should have given you a warning.
The variable $foul you use here:
> {
> print "$foul\n";
> }
Is the one you declared near the top of the file. The subroutine should
look more like:
sub foul_parser ($) {
my $foul = shift;
print "$foul\n";
}
The arguments are passed in the @_ array, and shift() will give you the
first element of that array.
And the same sort of set-up will also fix your other subs.
sub foo ($$) {
my ($first, $second) = @_;
}
--
Steve
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]