Академический Документы
Профессиональный Документы
Культура Документы
/usr/local/bin/perl
#o/p
#$ program7_2
#Here is some input.
#Here are some more words.
#Here
is my last line.
#^D
#Total number of words: 14
##############################################################################
#####
##############################################################################
#####
##############################################################################
#####
######## A program that counts the number of input lines containing the word the.
$thecount = 0;
print ("Enter the input here:\n");
$line = <STDIN>;
while ($line ne "") {
if ($line =~ /\bthe\b/) {
$thecount += 1;
}
$line = <STDIN>;
}
$pattern = <STDIN>;
chop ($pattern);
$filename = $ARGV[0];
$linenum = $matchcount = 0;
print ("Matches found:\n");
while ($line = <>) {
$linenum += 1;
if ($line =~ /$pattern/) {
print ("$filename, line $linenum\n");
@words = split(/$pattern/, $line);
$matchcount += @words - 1;
}
if (eof) {
$linenum = 0;
$filename = $ARGV[0];
}
}
if ($matchcount == 0) {
print ("No matches found.\n");
} else {
print ("Total number of matches: $matchcount\n");
}
#another
#Matches found:
#file1, line 2
#file2, line 2
#Total number of matches: 2
##############################################################################
#####
##############################################################################
#####
######## A program that loops using a pattern.
while ("balata" =~ /.a/g) {
$match = $&;
print ("$match\n");
}
#ba
#la
#ta
##############################################################################
#####
########A simple white space cleanup program.
@input = <STDIN>;
$count = 0;
while ($input[$count] ne "") {
$count++;
}
#^D
#Formatted text:
#This is a line of input.
#Here is another line.
#This is my last line of input.
##############################################################################
#####
######## ### Pattern Matching ###
## Sentences with three words, three word fields separated by white space
$pattern = '\w+\s+\w+\s+\w+';
## Find sentences with two "e" letters, and possible stuff between
$pattern = 'e.*e';
##############################################################################
#####
############ Marking Regular Expression Sub-strings and Using Substitution
## Search for two digits in same line, and switch their positions
foreach(@strings)
{ $pattern = '(\d)(.*)(\d)';
if (/$pattern/)
{ print "Grabbed pattern: $pattern \$1 = $1 \$2 = $2 \$3 = $3\n";
s/$pattern/$3$2$1/;
}
}
##############################################################################
#####
########
##############################################################################
#####
########
# change all occurances of a string in a file to another string
#
if ($#ARGV != 3) {
print "usage: chstring oldfile newfile oldstring newstring\n";
exit;
}
$oldfile = $ARGV[0];
$newfile = $ARGV[1];
$old = $ARGV[2];
$new = $ARGV[3];
open(OF, $oldfile);
open(NF, ">$newfile");
close(OF);
close(NF);
##############################################################################
#####
########