Sie sind auf Seite 1von 5

EEE 274 ADVANCED TIMING ANALYSIS

FALL 2010

PROJECT – I

DATE: 10/20/2010

INSTRUCTOR: PREPARED BY:

Dr. JING PANG Madhusudhan Nukhathoti


Arjun Vishwanathan Raju
Part 1.

Write a perl program to copy the content of “gpa.txt” into another new text file called
“gpa_copy.txt”.
In addition, on the screen, print out the rows of “gpa.txt” which includes 4.0 GPA.
# gpa.txt
Name GPA
Asd 4.0
Sdf 3.2
Fghsd 3.6
Qwer 4.0

Program Code:

#!user/local/bin/perl
#$s=shift @ARGV;
$s="gpa.txt";
#$d=shift @ARGV;
$d="gpa_copy.txt";
open(KK,$s);
@lines=<KK>;
#print @lines;
print "\n\n";
open(IN,'<',$s) or die "cant read source file\n";
open(OUT,'>',$d) or die "cant read dest file\n";
while(<IN>)
{
print OUT;
}
close (IN);
close (OUT);

sub patt
{
foreach (@lines)
{ print "$_\n" if /$pattern/;
}
#print "\n";
}
$pattern= '[4]';
&patt;
close (KK);
Output:

Asd 4.0

Qwer 4.0

Result:
The required result was copied in the file “gpa_copy.txt” and was displayed in the output.

PART 2
Use the following regular expression for pattern matching, show the results and give a short
discussion
of the functionality of the regular expression from (1) to (9)
sub print_array
{ for ($i=0; $i<=$#strings;$i++)
{print $strings[$i], "\n";
}
print "\n\n";
}
sub grep_pattern
{ foreach (@strings)
{print "$_\n" if /$pattern/;
}
print "\n\n";
}
### Setting up the Array of strings
@strings = ("eee, 2, 7, four", "Perl is great", "1, Three", "Five, 7",
"This is exercise in Perl");
(1). $pattern = '\d';
(2). $pattern = '\d+.+';
(3). $pattern = '\d+$';
(4). $pattern = 'e+'
(5). $pattern = 'e*'
(6). $pattern = 'e?'
(7). $pattern = '\bis\b'
(8). $pattern = '\b \w{4} \b'
(9). $pattern = '^[A-Z] '

Program Code:

#!usr/local/bin/perl
@strings=("eee, 2, 7, four", "Perl is great", "1, Three", "Five, 7", "This is exercise in Perl");
sub print_array
{
print "\n\n";
for ($i=0; $i<=$#strings; $i++)
{ print $strings[$i], "\n";
}
print "\n\n";
}

sub grep_pattern
{
foreach (@strings)
{ print "$_\n" if /$pattern/;
}
print "\n\n";
}
&print_array;
$pattern = '[2]';
&grep_pattern;

Output:

(1). $pattern = '\d';

Displays lines containing digits

eee, 2, 7, four
1, Three
Five, 7

2. $pattern = '\d+.+';

Displays lines containing one or more occurrences of digits

eee, 2, 7, four
1, Three

(3). $pattern = '\d+$';

Displays lines starting with any character and ending with digits

Five, 7

4). $pattern = 'e+'

Displays lines containing one or more occurrences of letter ‘e’


eee, 2, 7, four
Perl is great
1, Three
Five, 7
This is exercise in Perl
(5). $pattern = 'e*'

Display lines that have zero or more occurrences of letter ‘e’

eee, 2, 7, four
Perl is great
1, Three
Five, 7
This is exercise in Perl

(6). $pattern = 'e?'

Displays lines that have one or more occurrence of letter ‘e’.

eee, 2, 7, four

Perl is great
1, Three
Five, 7
This is exercise in Perl

(7). $pattern = '\b is \b'

Displays lines that have the word ‘is’

Perl is great

This is exercise in Perl

(8). $pattern = '\b \w{4} \b'

Display lines with a word boundary of 4 words or more


This is exercise in Perl

(9). $pattern = '^[A-Z] '

Displays the lines starting and ending with alphabets…

Perl is great
Five, 7
This is exercise in Perl

Das könnte Ihnen auch gefallen