Perl Lab Assignment 1
Perl Lab Assignment 1
ASSIGNMENT-1
Addition of two numbers
ASSIGNMENT-2
ASSIGNMENT-3
OUTPU
ASSIGNMENT-4
WAP to find out maximum and minimum no. from array, take input from user
{
$min = $_ if !$min || $_ < $min;
$max = $_ if !$max || $_ > $max;
}
print "min: $min\n";
print "max: $max\n";
<STDIN>
OUTPUT:
ASSIGNMENT-5
WAP to calculate average, mean, median, mode, take input from user
print "@array";
$mean=$sum/$a;
print "Mean is: $mean\n";
for($i=0;$i<$a-1;$i++)
{
for($j=$i+1;$j<$a;$j++)
{
if(@array[$i]>@array[$j])
{
$temp=@array[$i];
@array[$i]=@array[$j];
@array[$j]=$temp;
}
}
}
print "@array";
$l=$a/2;
$m=$a-1;
if($a%2==0)
{
$med=(@array[$l]+@array[$m/2])/2;
}
else
{
$med=@array[$l];
}
}
}
$c=0;
for($i=0;$i<$a;$i++)
{
if(@array[$i]==@b[$i])
{
$c++;
}
}
if($c==$a)
{
print "\nThere is no mode";
}
else
{
print "Mode is: "
for($i=0;$i<$k;$i++)
{
print "@b[$i]";
}
}
<STDIN>
OUTPUT:
ASSIGNMENT-6
WAP take input from user count no. of character, spaces, words
print "Enter The file Name:";
$a=<>;
open(FILE,$a)||diw("Cannot open file!");
$total_nlines = 0;
$total_nwords = 0;
$total_nletters = 0;
while ($line = <FILE>)
{
@words = split(" ",$line);
$nwords = @words;
OUTPUT:
ASSIGNMENT-8
Write the subroutine that takes the string as argument and return the string in reverse order by
word.
print "Enter The String To Reverse:";
@rev=fun($a);
print "\nAfter revese: @rev";
sub fun
{
$s=<>;
@array=split(undef,$s);
$cnt=0;
foreach(@array)
{
$cnt++;
}
for($i=0,%j=$cnt-1;$i<$cnt/2;$i++,$j--)
{
$temp=$array[$i];
$array[$i]=$array[$j];
$array[$j]=$temp;
}
return @array;
}
<STDIN>
OUTPUT:
ASSIGNMENT-9
Write a script that copies content of one file into another
print "Enter 1st File Name:";
$a=<>;
open(DATA1,"<$a")||diw("Cannot open file!");
print "Enter 2nd File Name to Write: ";
$b=<>;
open(DATA2, ">$b")||diw("Cannot open file!");
while(<DATA1>)
{
print DATA2 $_;
}
print "Aftre Copying: ";
close( DATA1 );
close( DATA2 );
<STDIN>
OUTPUT:
ASSIGNMENT-10
Write a script for the file which having some data. Find out the computer word from that data
and print how many times this word is occurred. */
foreach(@array)
{
if(@array[$i] eq '$b')
{
$cnt++;
}
}
}
print "\n$b ocurance in file: $cnt";
close(FILE);
<STDIN>
OUTPUT:
ASSIGNMENT-11
Write program to accept file name from command line. The file contains line of text where each
line is persons name. Convert all the letters of names in uppercase & display sorted list
close( DATA1 );
close( DATA2 );
<STDIN>
OUTPUT:
ASSIGNMENT-12
WAP to test function that computes the median of a given array. Even & odd array must be
check. If the array length is odd middle element of sorted array as median else for even length
array the median is the average of two middle element of sorted array.
{
for($i=0;$i<$a-1;$i++)
{
for($j=$i+1;$j<$a;$j++)
{
if(@array[$i]>@array[$j])
{
$temp=@array[$i];
@array[$i]=@array[$j];
@array[$j]=$temp;
}
}
}
$cnt=0;
foreach(@array)
{
$cnt++;
}
print "@array";
$l=$cnt/2;
$m=$cnt-1;
if($cnt%2==0)
{
$med=(@array[$l]+@array[$m/2])/2;
}
else
{
$med=@array[$l];
}
return $med;
}
<STDIN>
OUTPUT: