Perl - Array

perl

How can we create an array?

@arrayName = ("camel", "lama", "alpha");
@arrayName = qw(camel, lama, alpha);

How can we access a particular element of an array?

$arrayName[$index]

How can we remove the first element from an array?

($e, @fred) = @fred;
$x = shift(@arrayName);

How can we determine the length of an array?

$a = @fred

How can we extract parts of an array?

my $childPath2 = join('.', @parts[$i .. $#parts])

In the above code, assume that @parts is an array that is already exist, and $i is an arbitrary number. $#parts represent the last index of the @parts array. The above code extracts elements starting from $i to the end of the @parts array.

How can we swap the first two elements of an array?

@fred[0,1] = @fred[1,0]

How can we access the first element of the array without removing it from the array?

$a = $arrayName[0]
($a) = @arrayName;

How can we determine the index of the last element of an array?

$#fred

How can we put 8 and 9 in the beginning and at the end of an array?

@arrayName = (8, @arrayName, 9);

How can we access the last element of an array?

$arrayName[-1]

How can we truncate an array to a certain length?

$#arrayName = n;

$#arrayName is the index of the last element of an array. $#arrayName = 2 truncate the array to 3 elements.

How can we push a value onto an array?

push(@arrayName, $value);

How can we remove the last element from an array?

$oldValue = pop(@arrayName);

How can we add an element to the front of an array?

unshift(@arrayName, $a)

How can we sort an array numerically?

@result = sort { $a <=> $b } @somelist;

How can we sort an array alphabetically?

@result = sort { $a cmp $b } @somelist;

How can we sort an array?

@sorted = sort cmp_routine(@unsorted);
sort SUBNAME LIST;
sort BLOCK LIST;
sort LIST;

sub by_mostly_numeric {
    ($a <=> $b) || ($a cmp $b);
}

How can we revert the order of an array?

@b = reverse(@a);

How can we remove the first n elements from an array?

@oneslice = shift(@mylist, numberOfElementToShift);

How can we manipulate the content of an array using a range of indices?

@arrayName[1..4] = (1,4,9,6);
@arrayName[1,49,9,16,4] = (1, 7, 3, 4, 2); // non-sequential indices
@arrayName[@b] = (1, 0.25, 111, 625); // indices in another array

How can we join arrays?

join "separator", @arrayName;

How can we create a reference to an array?

$arrayref = [1,2,['a','b','c']];    // create a reference to an anonymous array of 
                        // three elements whose last element is itself
                        // a reference to another anonymous array

$arrayref = \@arrayName;

How can we create a list of references?

@list = (\$a, \$b, \$c);    // creating a list of references
@list = \($a, $b, $c);        // reference to a list is the same as creating a list of reference

\(@foo)    // return a list of references to the content of @foo, not a reference to @foo itself.

How can we access an array reference?

$arrayref->[0]
$arrayref[$x][$y][$z] = value;
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License