Online compiler

                Never    
8. a) Write a Perl script to find the largest number among three numbers.

Aim: To Write a Perl script to find the largest number among three numbers.

Recommended Hardware / Software Requirements:
Hardware Requirements: Intel Based desktop PC with minimum of 166 MHZ or faster 
processor with at least 64 MB RAM and 100 MB free disk space.
A working computer system with either Windows or Linux
A web browser either IE or Fire Fox
ActivePerl-5.24.1.2402-MSWin32-x86-64int-401627
Prerequisites: Student must know how to install Perl and How to set path.
Program:
#!/usr/bin/perl
print "Enter a number\n";
chomp($a=<stdin>);
print "Enter second number\n";
chomp($b=<stdin>);
print "Enter third number\n";
chomp($c=<stdin>);
$big=0;
$equal=0;
if($a eq $b)
{
$big = $a;
$equal = $a;
}
elsif($a > $b)
{
$big=$a;
}
else
{
$big = $b;
}
if($equal eq $c)
{
print "All numbers are same";
}
elsif($big < $c)
{
$big = $c;
}
else
{
print "The biggest number is $big \n";
}


8. b) Write a Perl script to print the multiplication tables from 1-10 using subroutines.

Aim: To Write a Perl script to print the multiplication tables from 1-10 using subroutines.

Recommended Hardware / Software Requirements:
Hardware Requirements: Intel Based desktop PC with minimum of 166 MHZ or faster 
processor with at least 64 MB RAM and 100 MB free disk space.
A working computer system with either Windows or Linux
A web browser either IE or Fire Fox
ActivePerl-5.24.1.2402-MSWin32-x86-64int-401627
Prerequisites: Student must know how to install Perl and How to set path.
Program: Design the Perl program and give the related input.
use strict;
use warnings;
use diagnostics;
use 5.01000;
&table( 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 );
sub table 
{
 my $loop;
 foreach $loop (@_) 
 {
 warn " loop $loop \n";
 for ( my $i=1; $i <= 10 ; $i++ ) 
 {
 my $ans = $i * $loop;
 print "$ans ";
 }
 print"\n";
 }
}
Input
1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
3 6 9 12 15 18 21 24 27 30
4 8 12 16
20 24 28 32 36 40
5 10 15
20 25 30 35 40 45 50
6 12 18
24 30 36 42 48 54 60
7 14 21
28 35 42 49 56 63 70
8 16 24
32 40 48 56 64 72 80
9
18 27 36 45 54 63 72 81 90
10 20 30 40 50 60 70 80 90 100



9. Write a Perl program to implement the following list of manipulating functions

a) Shift

b) Unshift
c) Push
Aim: To write a Perl program to implement the manipulating functions
a) Shift
b) Unshift
c) Push
Recommended Hardware / Software Requirements:
Hardware Requirements: Intel Based desktop PC with minimum of 166 MHZ or faster 
processor with at least 64 MB RAM and 100 MB free disk space.
A working computer system with either Windows or Linux
A web browser either IE or Fire Fox
ActivePerl-5.24.1.2402-MSWin32-x86-64int-401627
Prerequisites: Student must know how to install Perl and How to set path.
Input: Give the input based on the related program
Program:
push
The push function can add one or more values to the end of an array
#!/usr/bin/perl -w
$, = ",";
@array = ( 1, 2 );
print "Before pushing elements @array \n";
push(@array, (3, 4, 5));
print "After pushing elements @array \n";
shift
If you imagine the array starting on the left hand side, the shift function will 
move the whole array one unit to the left. The first element will "fall off" the 
array and become the function's return value. (If the array was empty, shift
will return undef.)
#!/usr/bin/perl
@array = (1..5);
while ($element = shift(@array)) {
print("$element - ");
}
print("The End\n");
Executing the program....
$perl main.pl
1 - 2 - 3 - 4 - 5 - The End
unshift
This is the opposite operation of shift. unshift will take one or more values (or 
even 0 if that's what you like) and place it at the beginning of the array, 
moving all the other elements to the right.
You can pass it a single scalar value, which will become the first element of 
the array. Or, as in the second example, you can pass a second array and then 
the elements of this second array (@others in our case) will be copied to the 
beginning of the main array (@names in our case) moving the other elements 
to higher indexes.
!/usr/bin/perl -w
@array = ( 1, 2, 3, 4);
print "Value of array is @array\n" ;

unshift( @array, 20, 30, 40 );
print "Now value of array is @array\n" ;
Output:
Executing the program....
$perl main.pl
Value of array is 1 2 3 4
Now value of array is 20 30 40 1 2 3 4


10. a) Write a Perl script to substitute a word, with another word in a string.

Aim: To Write a Perl script to substitute a word, with another word in a string.

Recommended Hardware / Software Requirements:
Hardware Requirements: Intel Based desktop PC with minimum of 166 MHZ or faster 
processor with at least 64 MB RAM and 100 MB free disk space.
A working computer system with either Windows or Linux
A web browser either IE or Fire Fox
ActivePerl-5.24.1.2402-MSWin32-x86-64int-401627
Prerequisites: Student must know how to install and set the python.
Input: Give the input related to string functions .
$sentence = "Perl is great at manipulating strings, naturally.";
print "$sentence \n\n";
$replacement = "is totally awesome";
substr($sentence, 5, 8) = $replacement;
print "Substituting $replacement staring at 5 and going 8 words \n"; 
print "$sentence \n\n";


11. Write a Perl script to validate IP address and email address.

Aim: To Write a Perl script to validate IP address and email address.

Recommended Hardware / Software Requirements:
Hardware Requirements: Intel Based desktop PC with minimum of 166 MHZ or faster 
processor with at least 64 MB RAM and 100 MB free disk space.
A working computer system with either Windows or Linux
A web browser either IE or Fire Fox
ActivePerl-5.24.1.2402-MSWin32-x86-64int-401627
Prerequisites: Student must know how to install and how to set the path .
Program:
IP Address:
print "Enter IP Address:";
my $ip = <STDIN>;
if ( $ip =~ /(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})/)
{
print "yes match $1 $2 $3 $4\n";
}
else
{
print "no match\n";
}
Email address:
use strict;
use warnings;
use 5.010;
use Email::Address;
my $line = 'sl@gniindia.org Scripting Language <sl@gniindia.org> Text sl@gniindia.org ' ;
my @addresses = Email::Address->parse($line); 
foreach my $addr (@addresses) {
 say $addr;
}


12. Write a Perl script to print the file in reverse order using command line arguments

Aim: To Write a Perl script to print the file in reverse order using command line arguments.

Recommended Hardware / Software Requirements:
Hardware Requirements: Intel Based desktop PC with minimum of 166 MHZ or faster 
processor with at least 64 MB RAM and 100 MB free disk space.
A working computer system with either Windows or Linux
A web browser either IE or Firefox
ActivePerl-5.24.1.2402-MSWin32-x86-64int-401627
Prerequisites: Student must know how to install and set the path in perl.
Program: open( FILE, "<$file_to_reverse" )
or die( "Can't open file file_to_reverse: $!" );
@lines = reverse <FILE>;
foreach $line (@lines) {
# do something with $line
}
(OR)
#!/usr/bin/perl
# rcat - a program to display the contents of a file in reverse order.
# author - alvin alexander, devdaily.com.
@lines = <>;
print reverse @lines;
To run this Perl script from the Unix/Linux command line, first save it in a 
file named rcat, and then make it executable, like this:
chmod +x rcat
input: After that, assuming you have a file namednumbersthat 
hascontents like this:
1
2
3
4
5
if you run this command:
rcat numbers
OUTPUT:
5
4
3
2
1

13. Write a PHP script to print prime numbers between 1-50.

Aim: To Write a PHP script to print prime numbers between 1-50.

Recommended Hardware / Software Requirements:
Hardware Requirements: Intel Based desktop PC with minimum of 166 MHZ or faster 
processor with at least 64 MB RAM and 100 MB free disk space.
A working computer system with either Windows or Linux
A web browser either IE or Fire Fox
Xampp-control pannel
Sublime text-3
Prerequisites: Student must know how to install php and run the basic programs.
Program:
<?php
$count = 0 ;
$number = 2 ;
while ($count < 20 )
{
$div_count=0;
for ( $i=1;$i<=$number;$i++)
{
if (($number%$i)==0)
{
$div_count++;
}
}
if ($div_count<3)
{
echo $number." , ";
$count=$count+1;
}
$number=$number+1;
}
?>
Output:
2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47,


14. PHP script to

a. Find the length of a string.

b. Count no of words in a string.
c. Reverse a string.
d. Search for a specific string.
Aim: To Write a PHP script to
a. Find the length of a string
b. Count no of words in a string
c. Reverse a string
d. Search for a specific string
Recommended Hardware / Software Requirements:
Hardware Requirements: Intel Based desktop PC with minimum of 166 MHZ or faster 
processor with at least 64 MB RAM and 100 MB free disk space.
A working computer system with either Windows or Linux
A web browser either IE or Fire Fox
Xampp-control pannel
Sublime text-3
Prerequisites: Student must know how to install php and run the basic programs.
Program:
a.
<?php
echo strlen(“hello”);
?>
Output: 5
b.
<?php
echo str_word_count(“hello world”);
?>
Output: 2
c.
<?php
echo strrev(“hello world”);
?>
Output: !dlroW olleH
d.
<?php
echo strrchr(“hello world!”,”world”);
?>
Output: world!


15. Write a PHP script to merge two arrays and sort them as numbers, in descending 

 order.

Aim: Write a PHP script to merge two arrays and sort them as numbers, in descending order.
Recommended Hardware / Software Requirements:
Hardware Requirements: Intel Based desktop PC with minimum of 166 MHZ or faster 
processor with at least 64 MB RAM and 100 MB free disk space.
A working computer system with either Windows or Linux
A web browser either IE or Fire Fox
Xampp-control pannel
Sublime text-3
Prerequisites: Student must know how to install php and run the basic programs.
Program:
<?php
$a1=array(1,30,15,7,25);
$a2=array(4,30,20,41,66);
$num=array_merge($a1,$a2);
array_multisort($num,SORT_DESC,SORT_NUMERIC);
print_r($num);
?>
Output:
Array 
( [0] => 66 [1] => 41 [2] => 30 [3] => 30 [4] => 25 [5] => 20 [6] => 15 [7] => 7 [8] => 4 [9] => 1 )


16. Write a PHP script that reads data from one file and write into another file.

Aim: Write a PHP script that reads data from one file and write into another file.

Recommended Hardware / Software Requirements:

Hardware Requirements: Intel Based desktop PC with minimum of 166 MHZ or faster processor with at least 64 MB computer and 100 MB free disk space

A working computer system with either Windows or Linux

A web browser either IE or Fire Fox

Xampp-control pannel

Sublime text-3

Prerequisites: Student must know how to install php and run the basic programs.

Program:

<?php $file = "http://example.com/Song.txt"; $f =

fopen($file, "r+"); $line = fgets($f, 1000);

$file1 = "http://example.com/Song1.txt"; $f1 =

fopen($file1, "r+"); $line1 = fgets($f1, 1000);

if (!($line == $line1))

{

fwrite($f1,$line);

$line1 = $line;

};

print htmlentities($line1);

?>




Raw Text