Showing posts with label chomp. Show all posts
Showing posts with label chomp. Show all posts

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";

Miscellaneous Notes


perl -vthe version of perl running
(most recent from www.activestate.com)
perl -ccheck syntax but don't run
perl -wcheck program more thoroughly than -c
perl -e "code;"run one line program
perl -iuse perl to edit files
perl -d perl's debugger

#!/usr/bin/perl -wfirst 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 ".
"not be found. \n";
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);

STDINmy $SearchValue = <STDIN>; What user has typed
tr$DoSearch =~ tr/A-Z/a-z/;
translate value to lowercase
printprint "some text and $variable \n";
length$varLength = length($variable2);
openopen(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
chompchomp($TheLine);get rid of the line break
seekseek(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 Loopwhile() # while there are records in the file
{ $TheLine = $_; # save the line's contents
chomp($TheLine); # get rid of the line break

if($TheLine = "")
{next}; # don't count if the line is a blank
$LineCount++; # increment the line count
}; # end of block

while(1) # loop forever (1=true)
{ print "Y or N"; # show this line on the screen.
my $Search = ; # save what's typed
}; # end of block
Until Loopuntil ($var1 == $var2)
{ if ($var3 eq $var4)
{ do some statements }
}
If if ($variable eq "value")
{block of statements};
if ($TheLine = "")
{next} ; # don't count if the line is a blank
if ($Cntr == 0)
{print "zero"};
Unlessunless($DoSearch eq 'i') # If not 'i',
{ print "Enter I or Q.\n";
next; # Go back to 'while' for next try
}
Forfor ($i = 33;
$i <=126;
$i++)
{statements}
Lastif($SrchVal eq 'q') {last}
Nextif($TheLine = "") {next};



' vs "
'do not interpret (except for ' and \)
"interprets

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.