Hello all,

I seem to be stuck on subroutines.  I've read the Perl
for Beginners Wrox chapter on them, and I'm still a
bit lost.  My problem is passing a scalar to a
subroutine, doing calculations therein, and then
returning a value; or basically, subroutines in
general.

I started out with a simple exercise, parsing a file,
passing the matched line contained in $_ to a
subroutine, and then having the subroutine print the
line in question.  That went rather well.  Here's that
code:

#################################
# 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);
        }
 }      


############################
# 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)
{ 
 print "$foul\n";
}


I then moved on to the exercise that is giving me
difficulty, which begins the same way, but then
expands like so:  In the subroutine,have it tell me
whether or not the line matches the criterion and feed
the info back to the main function. Use a variable in
the main function to tally the total
fouls/rebounds/etc. and print the value at the end of
the program.

Here is the code for that one:

#################################
# Subroutine prototypes
################################

sub FoulParser($);
sub PrintFouls();


#############################
# Main Program
#############################

#!/usr/bin/perl

use warnings;
use strict;
 
open(STATS, "stats.txt") or die "statfile\n";
my %fouls; 
my ($type, $totalFouls, $player);
my $foul; 

my $total;

 while (<STATS>)
 {
        if ($_ =~ /(\w+) (Foul:) (\w+)/)
        #if ($_ =~ /Foul/)
        {
         $foul = "$_";
   %fouls =  FoulParser($foul); 
  }
        $totalFouls = PrintFouls();
print "Total Fouls, both teams: $total"; 
        }

############################
# Subroutine Definitions
###########################
 
# This subroutine parses the stats file for the
different types of fouls
# and puts them into a hash.  It then returns a hash
with the data. 
 
sub FoulParser($)
{ 
 my %foulsData;
 my %fouls;
 
         $fouls{$3}{$1}++;
   return %fouls;
}


# This subroutine uses foreach loops for printing out
the foultypes, 
# the player that fouled and the number of those type
of fouls an 
# individual player has.  


sub PrintFouls()
{
 my $totalFouls;
 foreach my $type (keys %fouls)
 {
  print "\n$type Fouls\n";
  foreach my $player (keys %{$fouls{$type}})
  {
        
   print "$player:$fouls{$type}{$player}\n";
         $totalFouls += $fouls{$type}{$player};
  }
 }
 #print "player: $fouls{$type}{$player}";
 return $totalFouls; 
} 

I think I've included extra variables where I don't
need them, but overall, I'm quite confused with
getting the program to do what I want it to do.  Any
suggestions?  Thanks in advance for your help. -stu

__________________________________
Do you Yahoo!?
SBC Yahoo! DSL - Now only $29.95 per month!
http://sbc.yahoo.com

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to