Array Functions

Domain Hosting image
Web Hosting
Dedicated server
ssl certificate
Web Design image
Email

Arrays area big part of the Perl language and Perl has a lot of functions to help you work with them. Some of the actions arrays perform, include deleting elements, checking for the existence of an element, reversing all of the the elements in an array, and sorting the elements. The list below shows the functions you can use with arrays.

defined(VARIABLE)
Returns true if VARIABLE has a real value and if the variable has not yet been assigned a value. This is not limited to arrays; any data type can be checked.

Eg.:
$a = 10;
if (defined($a))
{
print "defined\n";
}
else
{
print "Not defined\n ";
}

In this case variable $a has been defined it will display: defined, Removing the first line ($a=10;) will display: Not defined.

delete(KEY)
Removes the key-value pair from the given associative array. If you delete a value from the %ENV array, the environment of the current process is changed, not that of the parent.

Eg.:
%a=(" South"=> "Chennai ", "North'=> "Delhi");
delete($a("North " 1);
will now make % a= ("'South"=> "Chennai")

each (ASSOC ARRAY) -
Returns a two element list that contains a key and value pair from the given associative array. The function is mainly used so you can iterate over the associate array elements. A null list is returned when the last element has been read.

Eg.:
%a=(" South => " Chennai" " North " => " Delhi
while(($area, $cap).=each(%a))
print "$cap is in $area\n";
will display:
Chennai is in South
Delhi is in North

exists(KEY) - Returns true if the KEY is part of the specified associative array.

Eg.:
% a =("South "=>"Chennai", "North"=> "Delhi");
if (exists($a{" West"}))
{
print "Available\n";
}
else
{
print "Not available";
}
will display:Not available

join(STRING, ARRAY) Returns a string that consists of all of the elements of ARRAY joined together by STRING.

Eg.:
@rec = (' Rakesh", "is ","a ","boy");
$v = join("-",@rec);
print "$v\n";
will display: Rakesh-is-a-boy

keys(ASSOC ARRAY) - Returns a list that holds all of the keys in a given associative array. The list is not in any particular order.

Eg.:
%a=(" South " => " Chennai "North"=> "Delhi");
@var = keys(%a);
#return type is an array
print join(" ".@var);
will display:North South

map(EXPRESSION, ARRAY) - Evaluates EXPRESSION for every element of ARRAY. The special variable is assigned to each element of ARRAY immediately before EXPRESSION is evaluated.

Eg.:
@words = ("large", "small");
@now = map(uc,@words);
#uc-uppercase function
print join(" ",@now); will display: LARGE SMALL

pack(STRING, ARRAY) - Creates a binary structure, using STRING as a guide, of the elements of ARRAY.

Eg.:
$a = "7e";
$char pack(" c",hex($a)
print "ASCII code $a corresponds to $char\n";
will display: ASCII code 7e corresponds to ~

pop(ARRAY)

Returns the last value of an array. It also reduces the size of the array by one.

Eg.:
Ca = (1,2,3,4);
pop(@a)':
print join(" ",@a);
will display: 1 2 3

push(ARRAY1, ARRAY2) Appends the contents of ARRAY2 to ARRAY1. This increases the size of ARRAY1 as needed.

Eg.:
Ca = (1,2,3,4);
push(@a,5);
print join(" ",@a);
will display:1 2 3 4 5

reverse (A R RAY) Reverses the elements of a given array when used in an array context. When used in a scalar context, the array is converted to a string, and the string is reversed.

Eg.:
@a = (1,2,3,4);
print join(" ",reverse(@a));
will display:4 3 2 1

scalar (A R RAY) Evaluates the array in a scalar context and returns the number of elements in the array.

Eg.:
@a = (1,2,3,4);
print scalar(@a); will display: 4

shift (A R RAY) Returns the first value of an array. It also reduces the size of the array by one.

Eg.:
@a = (1,2,3,4); shift(@a);
print join(" ",@a);
will display: 2 3 4

sort (ARRAY) Returns a list containing the elements of ARRAY in sorted order.

Eg.:
@a = ("cat", "bell", "ant"),
print join("-", sort(@a));
will dsiplay:ant-bell-cat

splice(ARRAY1, OFFSET, LENGTH, ARRAY2) Replaces elements of ARRAY1 with elements in ARRAY2. It returns a list holding any elements that were removed. Remember that the $[ variable may change the base array subscript when determining the OFFSET value.

Eg.:
*a ('a','b','c','d');
@b (1,2,3);
splice(@a,2,1,@b);
print join(" ",@a);
will display: a b 1 2 3 d

split(PATTERN, STRING, LIMIT) Breaks up a string based on some delimiter. In an array context, it returns a list of the things that were found. In a scalar context, it returns the number of things found.

Eg.:
$a "This is Larry";
@b split /,$a);
# @b now contains 3 elements
print "$b[0]\n";
will display: This

unshift(ARRAY1, ARRAY2) Adds the elements of ARRAY2 to the front of ARRAY1. Note that the added elements retain their original order. The size of the new ARRAY1 is returned.

Eg.:
*a = (1, 2,3,4,5);
unshift(@a,O);
print join(" '',@a);
will display: 0 1 2 3 4 5

values(ASSOC ARRAY) Returns a list that holds all of the values in a given associative array. The list is not in any particular order.

Eg.:
%a=(" South " => "Chennai"
"North"=>" Delhi");
@v = values(%a);
print join(" ",@v);
will display: Chennai Delhi



Domain Name Search

www.

Copyright (C) 2007. Web Domain design hosting. All rights reserved.