Questions about this topic? Sign up to ask in the talk tab.

Perl/Basics/Variables and Data Types/Arrays

From NetSec
Revision as of 00:33, 19 July 2012 by Chantal21I (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Arrays (or lists) have elements. Typically an array in perl can contain anything - each element can be something different. An array element may be a hash, hash reference, scalar, or another array.

Arrays are prefixed by the @ character:

<syntaxhighlight lang="perl">my @messages = ("Hello world!\n","I like perl!\n"); print $messages[0]; print $messages[1]; print "Size of messages array: ". $#messages . "\n"; </syntaxhighlight>

You can access and modify array elements directly:

<syntaxhighlight lang="perl"> $messages[0] = "Hello world!\n"; </syntaxhighlight>

Helper Functions

join()

Join will compile an array into a scalar. Using the array example above, @messages, the following code will generate the string "Hello world!\n, I like perl!\n" as a scalar:

<syntaxhighlight lang="perl">my @messages = ("Hello world!\n","I like perl!\n"); my $joined_message = join(", ",@messages); print $joined_message;</syntaxhighlight>

split()

Split takes a scalar and converts it to an array using a delimiter. Using our string from earlier:

<syntaxhighlight lang="perl">my $joined_message = "Hello world!\n, I like perl!\n"; my @messages = split('/, /',$joined_message); print $messages[0]; print $messages[1]; print "Size of messages array: ". $#messages . "\n";</syntaxhighlight>

push()

The push() function is used to append an element or elements to the end of an array, similar to the push instruction in assembly and treats the array like a stack.

 
my @array;
push(@array,'element one');
push(@array,('element two','element three'));
 
You can also add to the end of an array with:
$array[$#array] = "new element";
pop()

The pop() function is similar to the pop instruction in assembly and treats the array like a stack.

my @array;
$array[$#array] = 1;
$popped = pop(@array);
 

The same affect can be acheived with:

$popped = $array[$#array--];
RPU0j.png Executing pop() on an array will delete the highest order array element.
unshift()

The unshift() function is like the inverse of the push() function and treats the array like a stack. In stead of pushing to the top of the stack, this function operates against the bottom of the stack.

my @array;
$array[0] = 1;
unshift(@array,0); # $array[0] now contains "0" and $array[1] now contains [1].
 
shift()

The shift() function is like the inverse of the pop() function and treats the array like a stack. In stead of popping from the top of the stack, this function operates against the bottom of the stack.

my @array = (0,1);
my $first_element = shift(@array); # $array[0] now contains one, and @array only contains one element
 
Warning: Executing shift() on an array will delete the lowest order array element, changing the index of all elements.