Stuart White <[EMAIL PROTECTED]> wrote:
:
:
: HEre is the code:
:
: Note: Though I have been told twice, maybe 3x that I
: do not need to declare the number of variables that I
: am passing, my book, Perl for Beginners, says I do,
: and, the program doesn't print anything when I leave
: the declarations out. This is in 5.8.0.
Only $count is file scoped. The other variables can
be in the while, though they're not really needed.
: Thanks in advance for your help. -stu
:
: ------------------------------------
: # Subroutine prototypes
: ################################
: sub FoulParser($$$);
As Jeff mentioned, you really don't need prototypes
in this situation.
: #############################
: # Main Program
: #############################
:
: use warnings;
: use strict;
:
: open(STATS, "stats.txt") or die "statfile\n";
As mentioned before the most common idiom
includes the error generated by perl. Don't
put "\n" on the end. It suppresses need
information.
open STATS, "stats.txt" or die "Cannot open 'stats.txt': $!";
: my $foul;
: my $foultype;
: my $player;
: my $count;
: my @stuff;
: while (<STATS>)
: {
: if ($_ =~ /(\w+) (Foul:) (\w+)/)
: {
: $foul = "$_";
: $player = $1;
: $foultype = $3;
: print "\nline to be passed: $foul";
:
: @stuff = FoulParser($foul, $player, $foultype);
: my ($space1, $space2) = @stuff;
: print "space1: $space1\n";
: print "space2: $space2\n";
if ($_ =~ /(\w+) (Foul:) (\w+)/) {
print "\nline to be passed: $_";
printf
"space1: %s\nspace2: %s\n",
FoulParser( $_, $1, $2 );
: }
: }
:
: print "type of foul count: $count";
Here $count is file scoped. In the subroutine
you are using a subroutine scoped $count, which is
different is a different variable.
: sub FoulParser($$$)
The name for this subroutine is misleading.
The parsing of $line was handled outside the
routine and its name does not indicate that it
is printing anything.
: {
: my ($line, $player, $typefoul) = @_;
: my $count;
Since $count is not returned from the
subroutine, it is unnecessary.
: my @stuff;
: print "selected line: $line";
: print "Player's Name: $player\n";
: @stuff = $player;
: print "Type of foul committed: $typefoul\n";
: @stuff = $typefoul;
@stuff will always $typefoul, so you may as
well return it and stop creating @stuff, which is
a really poor name for a variable anyway.
: $count = $typefoul;
: $count++;
: return @stuff;
The entire sub is equivalent to:
my( $line, $player, $foul_type ) = @_;
print "Selected line: $line\n";
print "Player's Name: $player\n";
print "Type of foul committed: $foul_type\n";
return $foul_type;
: }
[snipped - completely unnecessary quoting]
What I think you wanted to return was this:
return ( $player, $foul_type );
But that doesn't make sense since you
didn't process them in any way. Why pass
back variables you just passed in. What
I really think you ultimately want is a
hash with foul type counts and a report
for each line.
I would recommend that a routine which
returns a report not actually do the
printing. The script might look like:
use Data::Dumper;
my %fouls_count;
open STATS, "stats.txt" or die "statfile\n";
while ( <STATS> ) {
if ( /(\w+) Foul: (\w+)/ ) {
# $1 = player, $2 = foul type
print line_report( $1, $2 );
# count by foul type
$fouls_count{ $2 }++;
}
}
close STATS;
# This would need to be changed to a
# full blown report
print Dumper \%fouls_count;
sub line_report {
return sprintf
"Player's Name: %s\n" .
"\tType of foul committed: %s\n",
@_;
}
HTH,
Charles K. Clarkson
--
Head Bottle Washer,
Clarkson Energy Homes, Inc.
Mobile Home Specialists
254 968-8328
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]