Sunteți pe pagina 1din 13

Copying Files

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

# Open file to read


open(DATA1, "<file1.txt");

# Open new file to write


open(DATA2, ">file2.txt");

# Copy data from one file to another.


while(<DATA1>)
{
print DATA2 $_;
}
close( DATA1 );
close( DATA2 );

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

rename ("/usr/test/file1.txt", "/usr/test/file2.txt" );

This function rename takes two arguments and it just rename existing file

Deleting an exiting file


Here is an example which shows how to delete a file file1.txt using unlink function.

#!/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

print "content-type: text/html \n\n";#HTTP HEADER

# DEFINE AN ARRAY
@coins = ("Quarter","Dime","Nickel");

# PRINT THE ARRAY


print "@coins";
print "<br />";
print @coins;

OUTPUT:

[skcet@AK1-28 ~]$ perl perlarrays.pl


Quarter Dime Nickel
Quarter Dime Nickel

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");

# PRINT THE WHOLE ARRAY


print "@coins";

# PRINT EACH SCALAR ELEMENT


print "<br />";
print $coins[0]; #Prints the first element
print "<br />";
print $coins[1]; #Prints the 2nd element
print "<br />";
print $coins[2]; #Prints the 3rd element

OUTPUT:

[skcet@AK1-28 ~]$ perl arrayindexing.pl


Quarter Dime Nickel
Quarter
Dime
Nickel

CODING:
#!/usr/bin/perl

print "content-type: text/html \n\n";#HTTP HEADER

# DEFINE AN ARRAY
@coins = ("Quarter","Dime","Nickel");

# PRINT THE WHOLE ARRAY


print "@coins";

# PRINT EACH SCALAR ELEMENT


print "<br />";
print $coins[0]; #Prints the first element
print "<br />";
print $coins[-1]; #Prints the last element
print "<br />";
print $coins[-2]; #Prints 2nd to last element
OUTPUT:

[skcet@AK1-28 ~]$ perl arrayindexing2.pl

Quarter Dime Nickel


Quarter
Nickel
Dime

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

print "content-type: text/html \n\n";#HTTP HEADER

# DEFINE AN ARRAY WITHOUT QUOTES


@coins = qw(Quarter Dime Nickel);
print "@coins";

OUTPUT:

[skcet@AK1-28 ~]$ perl gauti.pl


Quarter Dime Nickel

Sequential Number Arrays


PERL offers a shortcut for sequential numbers and letters.

CODING:
#!/usr/bin/perl

print "content-type: text/html \n\n";#HTTP HEADER

# SHORTCUTS SAVE TIME


@10 = (1 .. 10);
@100 = (1 .. 100);
@1000 = (100 .. 1000);
@abc = (a .. z);

# PRINT 'EM TO THE BROWSER


print "@10<br />";
print "@100<br />";
print "@1000<br />";
print "@abc<br />";

OUTPUT:

[skcet@AK1-28 ~]$ perl sequentialarrays.pl

Quarter Dime Nickel


QuarterDimeNickel

Finding the length of an Array

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

print "content-type: text/html \n\n";#HTTP HEADER

@nums = (1 .. 20);
@alpha = ("a" .. "z");

# SCALAR FUNCTION
print scalar(@nums)."<br />";
print scalar(@alpha)."<br />";

# REDEFINE TO SCALAR
$nums = @nums;
$alpha = @alpha;

print "$nums<br />";


print "$alpha<br />";
print "There are $nums numerical elements<br />";
print "There are ".scalar(@alpha)." letters in the alphabet!";

OUTPUT:

[skcet@AK1-28 ~]$ perl findlength.pl


20
26
20
26
There are 20 numerical elements
There are 26 letters in the alphabet!

Adding and Removing Elements:

Adding elements is a breeze, we use the following functions to add/remove and


elements:
push() - adds an element to the end of an array.
unshift() - adds an element to the beginning of an array.
pop() - removes the last element of an array.
shift() - removes the first element of an array.

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

print "content-type: text/html \n\n"; #HTTP HEADER

# 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 />";

# BACK TO HOW IT WAS


print "@coins";
OUTPUT:

[skcet@AK1-28 ~]$ perl modifyarrays.pl

Quarter Dime Nickel Our original Array, 3 elements.


Quarter Dime Nickel Penny Add penny to the end.
Dollar Quarter Dime Nickel Penny Add Dollar to the beginning.
Dollar Quarter Dime Nickel Remove Penny.
Quarter Dime Nickel Remove dollar, back to the original!

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.

Slicing Array Elements:

There is no specific slice() function for slicing up elements of an array. Instead


PERL allows us to create a new array with elements of another array using array
indexing.

CODING:

#!/usr/bin/perl

print "content-type: text/html \n\n"; #HTTP HEADER

# SEQUENTIAL ARRAY
@nums = (1..200);
@slicenums = @nums[10..20,50..60,190..200];
print "@slicenums";

OUTPUT:

[skcet@AK1-28 ~]$ perl myrangefriend.pl

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

print "content-type: text/html \n\n"; #HTTP HEADER

# DEFINED STRINGS
$astring = "Rain-Drops-On-Roses-And-Whiskers-On-Kittens";
$namelist = "Larry,David,Roger,Ken,Michael,Tom";

# STRINGS ARE NOW ARRAYS


@array = split('-',$astring);
@names = split(',',$namelist);

# PRINT THE NEW ARRAYS


print @array."<br />";
print "@names";

OUTPUT:

[skcet@AK1-28 ~]$ perl split.pl

Rain Drops On Roses And Whiskers On Kittens


Larry David Roger Ken Michael Tom

JOIN () FUNCTION:
We can use the join() function to rejoin the array elements and form one long,
scalar string.

CODING:

#!/usr/bin/perl

print "content-type: text/html \n\n"; #HTTP HEADER

# A COUPLE OF ARRAYS
@array = ("David","Larry","Roger","Ken","Michael","Tom");
@array2 = qw(Pizza Steak Chicken Burgers);

# JOIN 'EM TOGETHER


$firststring = join(", ",@array);
$secondstring = join(" ",@array2);

# PRINT THE STRINGS


print "$astring<br />";
print "$string";

OUTPUT:

[skcet@AK1-28 ~]$ perl join.pl


David,Larry,Roger,Ken,Michael,Tom
Pizza Steak Chicken Burgers

STRING FORMATTING:

CODING:

#!/usr/bin/perl

print "content-type: text/html \n\n"; #HTTP HEADER

@array2 = qw(Pizza Steak Chicken Burgers);


$string = join("<br />",@array2);

print "$string";

OUTPUT:

[skcet@AK1-28 ~]$ perl stringformatting.pl


Pizza
Steak
Chicken
Burgers

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

print "content-type: text/html \n\n"; #HTTP HEADER

# TWO ARRAYS
@foods = qw(pizza steak chicken burgers);
@Foods = qw(Pizza Steak chicken burgers);

# SORT 'EM
@foods = sort(@foods);
@Foods = sort(@Foods);

# PRINT THE NEW ARRAYS


print "@foods<br />";
print "@Foods";

OUTPUT:

[skcet@AK1-28 ~]$ perl sortarrays.pl


burgers chicken pizza steak
Pizza Steak burgers chicken

2. CODING:

#!/usr/bin/perl

print "content-type: text/html \n\n"; #HTTP HEADER

@Foods = qw(Pizza Steak chicken burgers);

# TRANSFORM TO LOWERCASE
foreach $food (@Foods) {
push(@foods, "\L$food");
}

# SORT
@foods = sort(@foods);

# PRINT THE NEW ARRAY


print "@foods";

OUTPUT:

[skcet@AK1-28 ~]$ perl arsenal.pl


burgers chicken pizza steak
RESULT:
Thus the odd numbers in the list has been found out.
FINDING THE ODD NUMBERS IN A LIST

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).

Syntax forms of the Perl grep function:

grep BLOCK LIST


grep (EXPR, 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.

LIST – is a list of elements or an array

Coding:

#!/usr/bin/perl

use strict;
use warnings;

# initialize an array
my @array = qw(3 4 5 6 7 8 9);

# first syntax form:


my @subArray = grep { $_ & 1 } @array;
# second syntax form
#my @subArray = grep $_ & 1, @array;

print "@subArray\n";

OUTPUT:

[skcet@AK1-28 ~]$ perl gautiod.pl

3579

S-ar putea să vă placă și