Perl - Practical Extraction and Report Language
Perl is an interpreted scripting language.
Perl is optimised for scanning arbitrary text files and system administration.
It has built-in extended regular expression matching and replacement, a data-flow mechanism to improve security with setuid scripts and is extensible via modules that can interface to C libraries.
Installation :
Compiling method :
e.g perl hello.pl
1. Simple mathematical calculation program
2. Pattern Matching Program
3. System Command Accepting Program
4. Program for Counting Words, Lines and Characters in a File
5. Program for Printing all 5 letter words in Paragraph by giving as it as input
6. Program for Printing the words which are all end with "ly"
7. Program to find average length of words in a text.
#! usr/bin/perl
use strict;
open (FILE,"wel.txt") or die "error in open wel.txt";
my $TSentences=0;
my $TWords=0;
my $TLetters=0;
my $AvgWordLength=0;
my $AvgSentenceLength=0;
while(my $line=<FILE>)
{
my @words=split(" ",$line);
my $nwords=@words;#no of words in that line
for(my $i=0;$i< $nwords;$i=$i+1)
{
my @letters=split("",$words[$i]);
my $nletters=@letters;
$TLetters=$TLetters+$nletters;
}
$TWords=$TWords+$nwords;
$AvgWordLength=$TLetters/$TWords;
}
print("Total Words:$TWords\n");
print("Total letters:$TLetters\n\n");
print("The average word length (in characters):$AvgWordLength\n ");
close(FILE);
open (FILE,"wel.txt") or die "error in open wel.txt";
while(my $ch=getc(FILE))
{
if($ch eq "." || $ch eq "!" || $ch eq "?")
{
$TSentences=$TSentences+1;
}
}
$AvgSentenceLength=$TWords/$TSentences;
print("Total sentences:$TSentences\n");
print("The average sentence length (in words):$AvgSentenceLength \n");
exit(0);
8. Program to count the frequency of words in text
# Clear word hash
%words = ();
print "What is the text you wish to analyze? \n";
print "Please include the filename and extension \n\n";
$textfile = <>; # Read the filename from the keyboard input
chop $textfile; # Remove line return
print "Now analyzing lexical frequency in $textfile\n\n";
open text_in, "< $textfile";
while ($line = <text_in>) {
chomp($line); # Remove line returns
$line = lc($line); # Change to lower case
$line = ' ' . $line . ' '; # Add spaces
$line =~ s/["(),;:.!?]/ /g; # Remove punctuation
@words = split (" ", $line ); # Put words into an array
foreach $word (@words) {
$words{$word} = $words{$word} + 1; # Put word and word frequency in hash
} #end foreach word
} #end while
close text_in;
# Display the results
foreach $word (sort keys %words) { # Sort the word hash
print "$word $words{$word}\n"; # Print word and frequency
}
Perl is an interpreted scripting language.
Perl is optimised for scanning arbitrary text files and system administration.
It has built-in extended regular expression matching and replacement, a data-flow mechanism to improve security with setuid scripts and is extensible via modules that can interface to C libraries.
Installation :
sudo apt-get install perl
Compiling method :
perl filename.pl
e.g perl hello.pl
1. Simple mathematical calculation program
#usr/bin/perl
$str = " Hello ";
print "cse:$str \n";
print "2+2 = ",2+2,"\n";
print "log(1e23) = ",log(1e23),"\n";
print "sin(3.141) = ",sin(3.141), "\n";
2. Pattern Matching Program
$_="hello how are you";
if(/hello/){
print "Default Variable = $_ \n";
print "Found hello \n";
}
# i modifier ignore case difference
if(m/HOw/i){
print "found how \n";
}
#match with negation
if(!/hall/){
print "Not found hall \n";
}
else {
print "found\n"; }
3. System Command Accepting Program
$time = localtime;
print "Time is now - $time \n";
print "Enter a shell command:";
$n = <STDIN>;
system $n;
4. Program for Counting Words, Lines and Characters in a File
#usr/bin/perl
$filen = "fileh.pl";
open($fh,"<",$filen);
while($line = <$fh>){
@lines = [split(/ +/,$line)];
$noline += scalar(@lines);
$words += scalar(split(/\w+/,$line));
$chars += length($line);
}
print "\n No.of Lines - $noline \n
No.of Words - $words \n
No.of Characters - $chars \n";
close($fh);
5. Program for Printing all 5 letter words in Paragraph by giving as it as input
print "Enter a text:";
while(<>){
chop;
tr/;:;.!?-//d;
foreach $w (split) {
if(length($w)==5) {
print "$w \n";
$score++;
}
}}
6. Program for Printing the words which are all end with "ly"
print "Enter the text:";
while (<>) {
foreach $wd(split){
if($wd =~ /ly$/i) {
print " $wd \n";
$var++;
}
}}
7. Program to find average length of words in a text.
#! usr/bin/perl
use strict;
open (FILE,"wel.txt") or die "error in open wel.txt";
my $TSentences=0;
my $TWords=0;
my $TLetters=0;
my $AvgWordLength=0;
my $AvgSentenceLength=0;
while(my $line=<FILE>)
{
my @words=split(" ",$line);
my $nwords=@words;#no of words in that line
for(my $i=0;$i< $nwords;$i=$i+1)
{
my @letters=split("",$words[$i]);
my $nletters=@letters;
$TLetters=$TLetters+$nletters;
}
$TWords=$TWords+$nwords;
$AvgWordLength=$TLetters/$TWords;
}
print("Total Words:$TWords\n");
print("Total letters:$TLetters\n\n");
print("The average word length (in characters):$AvgWordLength\n ");
close(FILE);
open (FILE,"wel.txt") or die "error in open wel.txt";
while(my $ch=getc(FILE))
{
if($ch eq "." || $ch eq "!" || $ch eq "?")
{
$TSentences=$TSentences+1;
}
}
$AvgSentenceLength=$TWords/$TSentences;
print("Total sentences:$TSentences\n");
print("The average sentence length (in words):$AvgSentenceLength \n");
exit(0);
8. Program to count the frequency of words in text
# Clear word hash
%words = ();
print "What is the text you wish to analyze? \n";
print "Please include the filename and extension \n\n";
$textfile = <>; # Read the filename from the keyboard input
chop $textfile; # Remove line return
print "Now analyzing lexical frequency in $textfile\n\n";
open text_in, "< $textfile";
while ($line = <text_in>) {
chomp($line); # Remove line returns
$line = lc($line); # Change to lower case
$line = ' ' . $line . ' '; # Add spaces
$line =~ s/["(),;:.!?]/ /g; # Remove punctuation
@words = split (" ", $line ); # Put words into an array
foreach $word (@words) {
$words{$word} = $words{$word} + 1; # Put word and word frequency in hash
} #end foreach word
} #end while
close text_in;
# Display the results
foreach $word (sort keys %words) { # Sort the word hash
print "$word $words{$word}\n"; # Print word and frequency
}
Post a Comment