References and Data Structures

Dereferencing: accessing individual array or hash elements using the arrow operator

The arrow operator enables us to directly access individual elements of an array or a hash through the reference to that array or hash.

In the examples below, we will use again the PLD1 data from the previous slides.

Accessing an array element

Let's say we want to access the third synonym of PLD1 (which is 'Choline phosphatase 1') and store it in a variable called $trivial_name.

$trivial_name = ${$ref_to_synonyms}[2];
However, this is cumbersome and confusing. Instead, let's use the alternative syntax for this purpose, which uses the arrow operator:
$trivial_name = $ref_to_synonyms->[2];
See how similar it is to the way we access individual elements of a simple array variable (e.g. @synonyms):
$trivial_name = $synonyms[2];
We just added the arrow.

Accessing a hash element

Considering the hash reference $ref_to_information, let's store the 'Function' of PLD1 (which is 'Signal transduction, membrane trafficking') in a variable called $function.

We can write:

$function = ${$ref_to_information}{'Function'};
But using the arrow operator is more elegant:
$function = $ref_to_information->{'Function'};
Indeed, it is similar to the way we access individual elements of a simple hash (e.g. %information):
$function = $information{'Function'};

Table of Contents.
Previous | Next.