Copying Files: #!/usr/bin/perl Rename ("/usr/test/file1.txt", "/usr/test/file2.txt")
Copying Files: #!/usr/bin/perl Rename ("/usr/test/file1.txt", "/usr/test/file2.txt")
Here is the example which opens an existing file file1.txt and read it line by line and generate another
copy file2.txt
#!/usr/bin/perl
Renaming a file
Here is an example which shows how we can rename a file file1.txt to file2.txt. Assuming file is available
in /usr/test directory.
#!/usr/bin/perl
This function rename takes two arguments and it just rename existing file
#!/usr/bin/perl
unlink ("/usr/test/file1.txt");
IMPLEMENTATION OF ARRAY FUNCTIONS IN PERL
AIM:
To implement array and various array functions in Perl.
DESCRIPTION:
Array:
Arrays are a special type of variable that store list style data types. Each object of
the list is termed an element and elements can either be a string, a number, or any type
of scalar data including another variable.
CODING:
#!/usr/bin/perl
# DEFINE AN ARRAY
@coins = ("Quarter","Dime","Nickel");
OUTPUT:
Array Indexing:
Each element of the array can be indexed using a scalar version of the same
array. When an array is defined, PERL automatically numbers each element in the array
beginning with zero. This phenomenon is termed array indexing. Elements can also be
indexed backwards using negative integers instead of positive numbers.
CODING:
#!/usr/bin/perl
print "content-type: text/html \n\n";#HTTP HEADER
# DEFINE AN ARRAY
@coins = ("Quarter","Dime","Nickel");
OUTPUT:
CODING:
#!/usr/bin/perl
# DEFINE AN ARRAY
@coins = ("Quarter","Dime","Nickel");
The qw Subroutine:
Quotations can be a hassle, especially if the array you wish to build has more
than 5 elements. Use this neat little subroutine to remove the need for quotes around
each element when you define an array.
Code:
#!/usr/bin/perl
OUTPUT:
CODING:
#!/usr/bin/perl
OUTPUT:
Retrieving a numerical value that represents the length of an array is a two step
process. First, you need to set the array to a scalar variable, then just print the new
variable to the browser as shown below.
There are two ways to set an array to scalar mode. We can use the scalar()
function or we can redefine the array as a scalar variable.
CODING:
#!/usr/bin/perl
@nums = (1 .. 20);
@alpha = ("a" .. "z");
# SCALAR FUNCTION
print scalar(@nums)."<br />";
print scalar(@alpha)."<br />";
# REDEFINE TO SCALAR
$nums = @nums;
$alpha = @alpha;
OUTPUT:
When adding elements using push() or shift() you must specify two arguments,
first the array name and second the name of the element to add. Removing an element
with pop() or shift() only requires that you send the array as an argument.
CODING:
#!/usr/bin/perl
# AN ARRAY
@coins = ("Quarter","Dime","Nickel");
# ADD ELEMENTS
push(@coins, "Penny");
print "@coins";
print "<br />";
unshift(@coins, "Dollar");
print "@coins";
# REMOVE ELEMENTS
pop(@coins);
print "<br />";
print "@coins";
shift(@coins);
print "<br />";
Array Functions:
Function Definition
push(@array, Element) Adds to the end of an array
pop(@array) Removes the last element of the array
unshift(@array, Element) Adds to the beginning of an array
shift(@array) Removes the first element of an array
delete $array[index] Removes an element by index number
It is also possible to remove any element by its indexed number.
CODING:
#!/usr/bin/perl
# SEQUENTIAL ARRAY
@nums = (1..200);
@slicenums = @nums[10..20,50..60,190..200];
print "@slicenums";
OUTPUT:
11 12 13 14 15 16 17 18 19 20 21 51 52 53 54 55 56 57
58 59 60 61 191 192 193 194 195 196 197 198 199 200
Transform Strings to Arrays:
With the split function, it is possible to transform a string into an array. To do this
simply define an array and set it equal to a split function. The split function requires two
arguments, first the character of which to split and also the string variable.
Coding:
#!/usr/bin/perl
# DEFINED STRINGS
$astring = "Rain-Drops-On-Roses-And-Whiskers-On-Kittens";
$namelist = "Larry,David,Roger,Ken,Michael,Tom";
OUTPUT:
JOIN () FUNCTION:
We can use the join() function to rejoin the array elements and form one long,
scalar string.
CODING:
#!/usr/bin/perl
# A COUPLE OF ARRAYS
@array = ("David","Larry","Roger","Ken","Michael","Tom");
@array2 = qw(Pizza Steak Chicken Burgers);
OUTPUT:
STRING FORMATTING:
CODING:
#!/usr/bin/perl
print "$string";
OUTPUT:
Sorting Arrays:
The sort() function sorts each element of an array according to ASCII Numeric
standards.
Because the sort() relies on ASCII Numeric values, problems can arise with
sorting capital letters and lower case letters.
1. CODING:
#!/usr/bin/perl
# TWO ARRAYS
@foods = qw(pizza steak chicken burgers);
@Foods = qw(Pizza Steak chicken burgers);
# SORT 'EM
@foods = sort(@foods);
@Foods = sort(@Foods);
OUTPUT:
2. CODING:
#!/usr/bin/perl
# TRANSFORM TO LOWERCASE
foreach $food (@Foods) {
push(@foods, "\L$food");
}
# SORT
@foods = sort(@foods);
OUTPUT:
AIM:
To find the odd numbers in a list
DESCRIPTION:
Perl grep function is used to filter a list and to return only those elements that
match a certain criteria - in other words it filters out the elements which don’t match a
condition. Normally, the function will return a list that contains less elements then the
original list (i.e. a sublist of the original list).
Where:
BLOCK – contains one ore more statements delimitated by braces; the last statement
in the block determines whether the block will be evaluated true or false. The block will
be evaluated for each element of the list and if the result is true, that element will be
added to the returned list. If you need to apply a more sophisticated filter that consists of
multiple code lines, you may consider to use the Perl grep function with a block.
EXPR – represents any expression that supports $_, in particular a regular expression.
The expression is applied against each element of the list and if the result of evaluation
is true, the current element will be appended to the returned list.
Coding:
#!/usr/bin/perl
use strict;
use warnings;
# initialize an array
my @array = qw(3 4 5 6 7 8 9);
print "@subArray\n";
OUTPUT:
3579