Perl
Perl
Output
b) Write a Perl script to print the multiplication tables from 1-10 using subroutines.
Program:
@array=(1..10);
$count=1;
while($count<11)
{
foreach $mul (@array)
{
$multiply=$count*$mul;
push(@multifinal,$multiply);
}
print"@multifinal\n";
@multifinal =() ;
$count++;
}
output:
18. Write a Perl program to implement the following list of manipulating functions
a) Shift
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.
# by shift function
b) Unshift
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.
# returned by unshift
output :
c) Push
# Initalizing the array
@x = ('Java', 'C', 'C++');
# Print the Inital array
print "Original array: @x \n";
# Pushing multiple values in the array
push(@x, 'Python', 'Perl');
# Printing the array
print "Updated array: @x";
output :
kits@kits-Veriton-M200-H310:~$ perl 181.pl
Original array: Java C C++
Updated array: Java C C++ Python Perl
19. a) Write a Perl script to substitute a word, with another word in a string.
use warnings;
use strict;
output:
use 5.010;
use Email::Address;
my @addresses = Email::Address->parse($line);
foreach my $addr (@addresses) {
say $addr;
}
kits@kits:~$ perl eads.pl
output :
[email protected]
dnk24@rmailcom
use Email::Valid;
my $address = Email::Valid->address('[email protected]');
print ($address ? 'yes valid' : 'no');
output
nokits@kits:~$ perl ep1.pl
yes valid
use strict;
use warnings;
use Email::Valid;
my @emails = (
'[email protected]',
'dnk at rediff.com',
'[email protected]',
'[email protected]',
'[email protected]',
);
output:
[email protected] agreed
dnk at rediff.com agreed
[email protected] agreed
[email protected] agreed
[email protected] agreed
if($ip =~ /(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/)
{
$ip = $1;
}
chomp($ip);
if($ip =~ m/^(\d\d?\d?)\.(\d\d?\d?)\.(\d\d?\d?)\.(\d\d?\d?)$/)
{
print("\nIP address found - $ip\n");
if($1 <= 255 && $2 <= 255 && $3 <= 255 && $4 <= 255)
{
print("Each octet of an IP address is ",
"within the range - $1.$2.$3.$4\n");
print("\n-> $ip IP address accepted!\n");
}
else
{
print("Octet(s) out of range. ",
"Valid number range between 0-255\n");
}
}
else
{
print("IP Address $ip is not in a valid format\n");
}
output:
20. Write a Perl script to print the file in reverse order using command line arguments
1
a
2
b
3
run the bellow command
output:
kits@kits:~$ cat sout
3
b
2
a
1