0% found this document useful (0 votes)
49 views3 pages

Simplified Perl Programmes (Maybe) .

The document contains 5 Perl scripts that perform various tasks: 1) A script that searches for a pattern in user input text and indicates if the pattern is found. 2) A script that counts the characters, words, and lines in user-provided text. 3) A script that sorts user-provided strings or numbers in ascending and descending order. 4) A script that checks if a user-provided number is prime or not. 5) A script that fits a linear regression line to data from a text file.

Uploaded by

P Naveen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views3 pages

Simplified Perl Programmes (Maybe) .

The document contains 5 Perl scripts that perform various tasks: 1) A script that searches for a pattern in user input text and indicates if the pattern is found. 2) A script that counts the characters, words, and lines in user-provided text. 3) A script that sorts user-provided strings or numbers in ascending and descending order. 4) A script that checks if a user-provided number is prime or not. 5) A script that fits a linear regression line to data from a text file.

Uploaded by

P Naveen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

1)

#!/usr/bin/perl
#search.pl

while (1)
{
print "Enter a string (say 15 chars; Ctrl-d to quit.)\n \t";
last unless (my $inp=<>);
print "Search for: ";
my $s=<>;
if ($inp =~/$s/i)
{
print "Pattern found (ignoring case). \n";
} else
{
print "Pattern not found.\n" ;
}

2)
#!/usr/bin/perl
#cwl.pl
#Counts the characters, words and lines in a file/input
print"enter paragraph:";
my @input = <>;
my $lines = @input;#array length is the no. of lines;
my $chars = 0; my $words = 0;
my $z='';
foreach $z (@input)
{
$chars+= length($z); #no. of characters in the text;
@loks = split(/[\t \,]+/,$z); #splits data when there is a tabspace or comma.
$words+=@loks;#no. of words in a line;
}
print "\n Chars=$chars; Words=$words; Lines=$lines\n";

3)

#!/usr//bin/perl
# sort.pl
# Sorts the given set of strings
print "Enter the strings to be sorted; (Ctrl-d to end input.)\n\t";
@lines=<>;
@slines=sort(@lines);

print "\nSorted list (Ascending) : \n" ;


foreach (@slines)
{
print $_; #prints the last variable
}
print "\nSortedulistu (Descending):\n";
foreach (reverse (@slines ))
{
print $_;
}

print "Enter the numbers to be sorted \n";


@i=<>;
print "the sorted numbers in ascending order is: \n ";
@all=sort{$b<=>$a}@i; # sort{$b<=>$a} is a function used to compare and swap values of input
(@i);
foreach(@all)
{
print $_;
}

print "\n";

4)
#!/usr/bin/perl
#prime.pl
#Program checks whether the input number is prime or not.
while (1)
{
printf ("Please enter an integer(Ctrl-d to exit): ");
last unless (my $num=<>); #without this infinite loop. this helps to terminate the while loop.
checks if the input is null
my $sign = $num < 0 ? 1 : 0 ; # Check is num less than 0. if true assign 1 else assign 0
$num=abs($num);
my $ul=sqrt ($num)+1;

if ($num <= 2)
{
$num ==2 ? print "Number is prime.\n":
print "Number is neither prime nor composite.\n";
}else
{
for ($i=2; $i<=$ul; $i++)
{
$isprime = $num % $i;
last unless $isprime; #breaks when isprime is 0. i.e the number is divisable by i.
}

$isprime ? print"$num is a prime number.\n":


print"$num is not a prime number.\n"; # is isprime true (ie 1) then print prime
number if false (ie 0) print not prime
}
}

5)
in terminal:
perl linreg.pl < data.txt
< sumbol means reads from the particular text file.
#!/usr/bin/perl
# linreg pl
# This program fits a line of the type y = mx+ c to the given data set.
$s = 0;$sx = 0;$sx2= 0;
$sxy = 0;$sy =0;
while ($inp=<>)
{
$inp =~ s/\s+//; #remove white spaces. \s+ means white spaces.
last unless length ($inp); #terminate the loop can be removed but then n should be reduced
by 1. last command is like break statement in c

@toks = split (/[\t \n \, ]+/,$inp) ; #Read in the data splits the data wherever there is a white
space, new line or a comma
$x=$toks [0] ;
$y=$toks [1] ;

$s+=1; #to get the total number of data points


$sx+=$x; $sx2+=($x*$x); #Accumulate the sums
$sxy+=($x*$y);$sy+=$y;
}

$delta = $s*$sx2-$sx*$sx;
$m = ($s*$sxy - $sx*$sy)/$delta; #Calculate m and c
$c = ($sx2*$sy -$sx* $sxy)/$delta ;

printf ("\n Data points (n): %12d\n",$s) ;


printf("Least-squares line : %4.2f * x +%-4.2f\n\n", $m, $c);

You might also like