Perl Array

We can access an element from a Perl Array using either positive integer (starting from 0) and negative integer. For negative integer, it’s counting will start from the end of the array. For example, index -1 will access the last element in the array whereas index -3 will access the third last element in the array.

Array can be defined in two ways, either using pw() or a list:

my @array = pw ( a b c );
my @array = (a, b, c);

We could also create an Array #Reference using square brackets []:

my $arrayRef = [ a, b, c ];

Note: It is possible to combine two Arrays into an Array, but that Perl will not retain the container, that is the original owner of the data. This means there is no way to know who owns the data in the bigger Array. One of the solution to this is using #Perl Reference.

There are two ways to know the length or the total number of elements of a Perl Array: scalar() and direct assignment. Using scalar() is a more expressive way that you could find out the length of an array. Just pass in the array that you need to know its length to the function and assigned its result to a variable like the following, so you can get the desired answer:

my @array = pw ( Hello World );
my $len = scalar(@array); # Result: 2

Directly assigning a Perl Array to a scalar variable (that is string and integer) could do the same thing as scalar() did, though less expressive. The example is shown below:

my @array = pw ( Hello World );
my $len = @array; # Result: 2

Note: Either case, the length or the total number of elements of a Perl Array does count undef# as a valid member. Therefore, it shouldn’t surprise you if the above-mentioned methods count undef elements in the total number of elements inside the array.

Sorting an array could be as simple as using the sort() function. Just pass in the array as the parameter of the function, then the function will sort the elements in alphabetical order and put them into a new array. This could be useful in sorting strings, but it is not suited for number ordering. As such, we could define the condition for the sorting algorithm {} for it to sort the array in a way we wanted. Both use cases are shown below:

my @str_array = pw ( apple orange calsium );
my @sorted_str_array = sort(@str_array);  # Result: apple, calsium, orange

my @num_array = pw ( 10 8 19 1 29 );
my @sorted_num_array_with_default = sort(@num_array); # Result: 10, 19, 1, 29, 8
my @sorted_num_array_with_custom = sort { @a<=>@b } (@num_array);
# Result: 1, 8, 10, 19, 29

Note: sort() will not alter the elements’ order in the original array.

To clear an array, assign array to an empty bracket like my @array = ();. Don’t define array as undef! It will fill up the array with undef elements which still counted as valid elements. An empty array returns an integer of 0, which is treated as false in terms of Perl Boolean#.

Links to this page
  • Perl Reference

    We could create a reference to a variable (could be Perl Array# or Perl Hash#) by using \ syntax such as follows:

  • Perl Hash

    Note: Be aware that Hash will be treated as list in other contexts such as Perl Array. Since there is no association between keys and values in a list, passing a Hash to an array will destroy the established relationship. One of the solution is to use #Perl Reference.

#perl #resource