#!/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";
Showing posts with label chomp. Show all posts
Showing posts with label chomp. Show all posts
Wednesday, March 10, 2010
Counting Example
Miscellaneous Notes
| perl -v | the version of perl running (most recent from www.activestate.com) |
| perl -c | check syntax but don't run |
| perl -w | check program more thoroughly than -c |
| perl -e "code;" | run one line program |
| perl -i | use perl to edit files |
| perl -d | perl's debugger |
| #!/usr/bin/perl -w | first line in code on Unix |
Variable Names: Case-sensitive. Any Length. A-z and Underscores. Can't start with a number.
| # | comment -- starts anywhere on the line | |
| = | set value on left to value on right | |
| ; | statement end. not required at the end of a block. | |
| {} | beginning and end of block. can be embedded. | |
| . | open(INFILE,$TheFile) or die "The file $TheFile could ". | concatenation |
| + | $variable = $variable + 1; | add values on right and store in variable on the left |
| $0 | the name of the program that is running | |
| $_ | $TheLine = $_; | Save the line's contents |
| \n | line break | |
| ++ | $LineCount++; | Increment value |
| /t | tab | |
| hex | \x42 = B | |
| octal | \102 = B | |
| chr | $CapB = chr(66); | |
| STDIN | my $SearchValue = <STDIN>; | What user has typed |
| tr | $DoSearch =~ tr/A-Z/a-z/; | translate value to lowercase |
| print "some text and $variable \n"; | ||
| length | $varLength = length($variable2); | |
| open | open(filehandle,$filename) or die("message to display if not found \n"); | Open file. If not able, post message |
| substr | $var = substr($Line,$PosFromZero,$Len); | substring |
| chomp | chomp($TheLine); | get rid of the line break |
| seek | seek(INDB,0,0); | Tells Perl where in the file to start |
| split | ($Var1, $Var2) = split(/\t/, $TheRec); | split a string based on a value into separate variables |
| While Loop | while( while(1) my $Search = |
| Until Loop | until ($var1 == $var2) |
| If | if ($variable eq "value") if ($TheLine = "") if ($Cntr == 0) |
| Unless | unless($DoSearch eq 'i') { print "Enter I or Q.\n"; next; } |
| For | for ($i = 33; $i <=126; $i++) |
| Last | if($SrchVal eq 'q') {last} |
| Next | if($TheLine = "") {next}; |
' vs "
'
"
print 'Lynn\'s programs are on usr\\user\\ltobias.';
Lynn's programs are on usr\user\ltobias.
$Shout = "Help!";
print "Please come when I shout \"$Shout\". \n";
Please come when I shout "Help!".
$Value = "something";
print "\$Value is $Value. \n";
$Value is something.
Subscribe to:
Posts (Atom)