Apr 12, 2008

Perl Array Functions

Perl Array Functions

Shows some Perl array functions and examples of how to use them.

Delete

%group = ('forest', 'tree', 'crowd', 'person');
delete($group{'forest'})
 
if (exists($group{'forest'}))
{
        print "The named element was not deleted.\n";
}
else
{
        print "The element was deleted.\n";
}
 

each

%group = ('forest', 'tree', 'crowd', 'person', 'fleet', 'ship');
 
while (($key, $value) = each(%group))
{
        print "A $value is part of a $key.\n";
}

Results printed are:

A tree is part of a forest.
A person is part of a crowd.
A ship is part of a fleet.

exists

%group = ('forest', 'tree', 'crowd', 'person');
if (exists($group{'crowd'}))
{
        print "The named element exists.\n";
}
else
{
        print "No element with that name found.\n";
}

grep

join

The elements in an array are joined into a single string with each element seperated by the value in EXPR.

join EXPR, ARRAY

Example:

@plantlist= (flower, tree, grass);
join " ", @plantlist;

This evaluates to "flower tree grass".

keys

Returns hash keys where:

%group = ('forest', 'tree', 'crowd', 'person');
foreach $key (keys %group) 
{
   print $key . "\n";
}

map

pop

push

reverse

scalar

shift

sort

splice

split

unshift

values

No comments:

Post a Comment

Popular Posts