Wednesday, March 10, 2010

Counting Example

#!/usr/bin/perl -w
use strict;
use diagnostics;

print "\n";
print "+---------------------------------------------------------------+ \n";
print "| Count characters and/or words and/or lines in a file. | \n";
print "+---------------------------------------------------------------+ \n\n";

#- 1 --------------------------------------------------------------------------
# Ask for filename
#------------------------------------------------------------------------------

print "File Name: "; # Display the question
my $TheFile = <stdin>; # Store what's typed in
chomp($TheFile); # Get rid of line break

open(INFILE, $TheFile) or # Open the file if it exists
die "The file $TheFile could not be found. \n";

#- 2 --------------------------------------------------------------------------
# Do they want to count characters
#------------------------------------------------------------------------------
CHARQUESTION:

print "Count Characters? (y/n) "; # Display the question
my $CountChars = <stdin>; # Store what's typed in
chomp($CountChars); # Get rid of line break
$CountChars =& tr/A-Z/a-z/; # Change 'Y/N' to 'y/n'

unless ($CountChars eq 'y' or 'n') # If not 'y' or 'n', ask again
{print "You must enter Y or N. \n";
goto WORDQUESTION;
}
my $CharCount = 0; # Initialize the counter

#- 3 --------------------------------------------------------------------------
# Do they want to count words?
#------------------------------------------------------------------------------
WORDQUESTION:

print "Count Words? (y/n) "; # Display the question
my $CountWords = <stdin>; # Store what's typed in
chomp($CountWords); # Get rid of line break
$CountWords =& tr/A-Z/a-z/; # Change 'Y/N' to 'y/n'

unless ($CountWords eq 'y' or 'n') # If not 'y' or 'n', ask again
{print "You must enter Y or N. \n";
goto WORDQUESTION;
}
my $WordCount = 0; # Initialize the counter

#- 4 --------------------------------------------------------------------------
# Do they want to count lines? (records)
#------------------------------------------------------------------------------
LINEQUESTION:

print "Count Lines? (y/n) "; # Display the question
my $CountLines = <stdin>; # Store what's typed in
chomp($CountLines); # Get rid of line break
$CountLines =& tr/A-Z/a-z/; # Change 'Y/N' to 'y/n'

unless ($CountLines eq 'y' or 'n') # If not 'y' or 'n', ask again
{print "You must enter Y or N. \n";
goto LINEQUESTION;
}
my $LineCount = 0; # Initialize the counter

#- 5 --------------------------------------------------------------------------
# While there are records in the file...
#-----------------------------------------------------------------------------
while(<infile>)
{ my $TheLine = $_; # Save the line's contents
$LineCount = $LineCount + 1; # Count Lines

#----------------------------------------------------------------------------
# count characters
#----------------------------------------------------------------------------
chomp($TheLine); # Get rid of the line break
if($TheLine eq "") { next }; # If line blank, get the next
my $LineLen = length($TheLine); # Get line length
$CharCount = $CharCount + $LineLen; # increment char count
$WordCount = $WordCount + 1; # This line has at least 1 word

#---------------------------------------------------------------------------
# Now loop through each character on this line to look for words
#----------------------------------------------------------------------------
my $CharPos = 0; # Position we are in the line

until($CharPos == $LineLen) # Check for line end;
{if(substr($TheLine, $CharPos, 1) eq " ") # if not, check for space
{ $WordCount = $WordCount + 1 }
$CharPos = $CharPos + 1;
} # End of until
} # End of while(<infile>) loop
#- 6 --------------------------------------------------------------------------
# Write A Small Sub Routine To Do The Same (from wondersky.com)
#------------------------------------------------------------------------------
sub comma_me {
local $_ = shift;
1 while s/^(-?\d+)(\d{3})/$1,$2/;
return $_;
}
#------------------------------------------------------------------------------
# All finished, so print out the results
#------------------------------------------------------------------------------

print "\n";
print "************************************* \n";
print "* $0 COMPLETED SUCCESSFULLY \n";
print "************************************* \n\n";
print "Counts For $TheFile: \n\n";

if ($CountChars eq "y")
{print "Number of characters: ", comma_me($CharCount), "\n"};
if ($CountWords eq "y")
{print "Number of words : ", comma_me($WordCount), "\n"};
if ($CountLines eq "y")
{print "Number of lines : ", comma_me($LineCount), "\n"};
print "\n";

No comments:

Post a Comment