b) Write a Perl script to print the multiplication tables from 1-10 using subroutines.
a) Write a Perl script to find the largest number among three numbers.
Source Code:
#!/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";
}
Output:
Enter a number
789
Enter second number
258
Enter third number
254
The biggest number is 789
...Program finished with exit code 0
Press ENTER to exit console.
b) Write a Perl script to print the multiplication tables from 1-10 using subroutines.
Source Code:
print "multiplication tables from 1-10 using subroutines\n";
&table(1);
&table(2);
&table(3);
&table(4);
&table(5);
&table(6);
&table(7);
&table(8);
&table(9);
&table(10);
sub table{
my $i = 1;
my $loop;
foreach $loop(@_){
for($i;$i<=10;$i++){
my $ans = $i*$loop;
print"$loop*$i=$ans \n";
}
print"\n";
}
}
Output:
multiplication tables from 1-10 using subroutines
1*1=1
1*2=2
1*3=3
1*4=4
1*5=5
1*6=6
1*7=7
1*8=8
1*9=9
1*10=10
2*1=2
2*2=4
2*3=6
18. Write a Perl program to implement the following list of manipulating functions
a)Shift
b)Unshift
c) Push
a)shiift
shift function
This function returns the first value in an array, removing it and shifting the elements of the array list to the left by
one. Shift operation removes the value like pop but is taken from the start of the array instead of the end as in pop.
This function returns undef if the array is empty otherwise returns first element of the array.
Syntax: shift(Array)
Source Code:
#!/usr/bin/perl
# Initializing the array
@x = ('Java', 'C', 'C++');
# Print the Initial array
print "Original array: @x \n";
# Prints the value returned
# by shift function
print "Value returned by shift: ",
shift(@x);
# Array after shift operation
print "\nUpdated array: @x";
Output:
Original array: Java C C++
Value returned by shift :Java
Updated array: C C++
b)Unshift
unshift function
This function places the given list of elements at the beginning of an array. Thereby shifting all the values in an
array by right. Multiple values can be unshift using this operation. This function returns the number of new
elements in an array.
Syntax: unshift(Array, List)
Source Code:
#!/usr/bin/perl
# Initializing the array
@x = ('Java', 'C', 'C++');
# Print the Initial array
print "Original array: @x \n";
# Prints the number of elements
# returned by unshift
print "No of elements returned by unshift: ",
unshift(@x, 'PHP', 'JSP');
# Array after unshift operation
print "\nUpdated array: @x";
output:
Original array: Java C C++
No of elements returned by unshift :5
Updated array: PHP JSP Java C C++
c) Push
push function
This function inserts the values given in the list at an end of an array. Multiple values can be inserted separated
by comma. This function increases the size of an array. It returns number of elements in new array.
Syntax: push(Array, list)
Source Code:
#!/usr/bin/perl
# Initializing the array
@x = ('Java', 'C', 'C++');
# Print the Initial array
print "Original array: @x \n";
# Pushing multiple values in the array
push(@x, 'Python', 'Perl');
# Printing the array
print "Updated array: @x";
output:
Original array: Java C C++
Updated array: Java C C++ Python Perl
9. a) Write a Perl script to substitute a word, with another word in a string.
b) Write a Perl script to validate IP address and email address.
a) Write a Perl script to substitute a word, with another word in a string.
substitution Operator or ‘s’ operator in Perl is used to substitute a text of the string with some pattern
specified by the user.
Syntax: s/text/pattern
Returns: 0 on failure and number of substitutions on success
Source Code:
#!/usr/bin/perl -w
# String in which text
# is to be replaced
$string = "GeeksforGeeks";
# Use of s operator to replace
# text with pattern
$string =~ s/for/to/;
# Printing the updated string
print "$string\n";
output:
GeekstoGeeks
b) Write a Perl script to validate IP address and email address.
Source Code:
#!/usr/bin/perl
my $ip = "MY IP ADDRESS IS172.26.39.41THIS IS A VALID IP ADDRESS";
if($ip =~ /(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/)
{
$ip = $1;
print "$ip\n";
}
Output:
20) Write a Perl script to print the file in reverse order using command line arguments
Source code:
#!/usr/bin/perl -w
# Defining string to be reversed
$string = "Hello World";
print scalar reverse("$string"), "\n";
$string = "Geeks For Geeks";
print scalar reverse("$string"), "\n";
output:
dlroW olleH
skeeG roF skeeG