< All Perl topicsPerl (3)Java (1)

Arrays

Arrays are a very useful data-type in all programming languages. Although their syntax and implementation (and therefore efficiency) vary between programming languages, the general tools and methods for manipulating them are very similar.

Perl:

Perl can only easily handle 1 dimensiona arrays.

Looping through an array with foreach

Last updated at 11:52 AM on Wednesday 5th September, 2007 by Administrator


Beware the ugly way:

foreach(@array)
{
  print $_;
}

It is prettier to assign the element of the array to named variable.

foreach my $element (@array)
{
  print $element;
}

Comments (0)

Creating an array

Last updated at 11:53 AM on Wednesday 5th September, 2007 by Administrator


my @array = (0, 1, 2, 3);


$array[1] = 2;

Comments (0)

Add to end of an array

Last updated at 08:38 PM on Monday 12th November, 2007 by Administrator


To add to the right-side:

@arr = ("a", "b");
push(@arr, "c");

To add to the left-side:

@arr = ("b", "c");
unshift(@arr, "a");

Comments (0)