Вы находитесь на странице: 1из 10

#!

/usr/local/bin/perl

######## A word-count program that handles multiple spaces between words.


$wordcount = 0;
$line = <STDIN>;
while ($line ne "") {
chop ($line);
@words = split(/ +/, $line);
$wordcount += @words;
$line = <STDIN>;
}
print ("Total number of words: $wordcount\n");

#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 simple variable-name validation program.

print ("Enter a variable name:\n");


$varname = <STDIN>;
chop ($varname);
if ($varname =~ /\$[A-Za-z][_0-9a-zA-Z]*/) {
print ("$varname is a legal scalar variable\n");
} elsif ($varname =~ /@[A-Za-z][_0-9a-zA-Z]*/) {
print ("$varname is a legal array variable\n");
} elsif ($varname =~ /[A-Za-z][_0-9a-zA-Z]*/) {
print ("$varname is a legal file variable\n");
} else {
print ("I don't understand what $varname is.\n");
}

#Enter a variable name:


#$result
#$result is a legal scalar variable
##############################################################################
#####

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

print ("Number of lines containing 'the': $thecount\n");


#Enter the input here:
#Now is the time
#for all good men
#to come to the aid
#of the party.
#^D
#Number of lines containing 'the': 3
##############################################################################
#####
######## A simple pattern-search program.
print ("Enter the search pattern:\n");

$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");
}

#perl current_program.pl file1 file2


#Enter the search pattern:

#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 "") {

$input[$count] =~ s/^[ \t]+//;

$input[$count] =~ s/[ \t]+\n$/\n/;

$input[$count] =~ s/[ \t]+/ /g;

$count++;
}

print ("Formatted text:\n");


print (@input);

#This is a line of input.


# Here is another line.
#This

is my last line of input.

#^D
#Formatted text:
#This is a line of input.
#Here is another line.
#This is my last line of input.

##############################################################################
#####
######## ### Pattern Matching ###

## Find the word "Perl"


$pattern = 'Perl';

## Find "Perl" at the beginning of a line


$pattern = '^Perl';

## Find sentences that contain an "i"


$pattern = 'i';

## Find words starting in "i", i.e. a space preceeds the letter


$pattern = '\si';

## Find strings containing a digit


$pattern = '\d';

## Search for a digit followed by some stuff


$pattern = '\d+.+';

## Find strings with a digit at the end of a line


$pattern = '\d+$';

## Search for a digit, possible stuff in between, and another digit


$pattern = '\d.*\d';

## Find four-letter words, i.e. four characters offset by word boundaries


$pattern = '\b\w{4}\b';

## 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

## Find five-letter words and replace with "Amazing"


$pattern = '\b\w{5}\b';
foreach(@strings)
{s/$pattern/Amazing/;
}

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

# read in each line of the file


while ($line = <OF>) {
$line =~ s/$old/$new/;
print NF $line;
}

close(OF);
close(NF);
##############################################################################
#####
########

Вам также может понравиться