How to access array element in Perl

Task. In Perl, the program has an array. You need to read the value of one of the array elements.

Here is an example of how to do it:

▶ Run
#!/usr/bin/perl

my @arr = ('one', 'two', 'three');

print $arr[1];

If you save the text of this program into a file script.pl and run perl script.pl, the screen will appear the text two.

So, what's going on here.

First, we created an array named @arr. Variable-an array begins with the character @, this symbol is similar to the first a letter from the word "array". We have created an array and has placed in 3 of the item. In our example, all 3 items is line, but all in a single array can contain items of very different types, e.g., strings, numbers and references.

Each element in the array has its own serial number. This number is called "index". In a programming language Perl and many other programming languages the elements of the array be numbered starting with number 0. In the example in the array contains 3 elements:

  • string 'one' is the array element with index 0
  • string 'two' is the array element with index 1
  • string 'three' is the array element with index 2

In order to access the array element with index 1 you need to write $arr[1]. Write the name of the array and square parentheses write the index of the item which we want to retrieve. Note one feature. When we turn one element of the array, instead of a symbol @ in the name of the array you need to write the symbol $ (the dollar sign, the symbol is similar to the first letter in the word "scalar"). The idea was that with such records, we clearly emphasize that we need to get one value, but, in my opinion, this is a design error language. It does not simplify, but on the contrary made all the more difficult. In a programming language Perl6 is fixed, but in Perl5 when accessing the array element you need to use the dollar sign $: $arr[1].

Refer to the last element in the array

When you create an array @arr appears automatically variable $#arr which contains the index of the last element. When adding/removing elements from the array to the variable automatically changes (and even you can own record the number in this variable).

To access the last element of the array you can use the recording $arr[$#arr]. But easier use $arr[-1]. Index -1 means the index of the last element in the array, the index -2 — the penultimate, etc.

Wrong way

Here's the code (carefully, this contains the error):

▶ Run
#!/usr/bin/perl

my @arr = ('one', 'two', 'three');

print @arr[1]; # error

This option is just as correct, but instead correct to access an element of array using $arr[1]here used @arr[1].

If you save the text of this program into a file script.pl and run it perl script.pl, the screen will be text two. String 'two', this is indeed the element with index 1 in the array @arr, so at first glance, the error is not visible.

But if you add program code line use warnings; (which is almost always worth doing), then the output will be visible warning:

Scalar value @arr[1] better written as $arr[1] at script.pl line 7.
two

Form @arr[ ] — is getting multiple values from the array. It's called "cut". A few examples:

  • @arr[1,2] — returns a list of two elements, the array elements with indexes 1 and 2
  • @arr[0..2] — returns a list of three elements, the array elements with indexes 0, 1 and 2
  • @arr[1, 1] — returns a list of two elements, repeats the element at index 1 twice

Entry @arr[1] — returns a list of one element, plus issues a warning.

Entry @arr[1,] — returns a list of one element without the warning.

If you need the value of one element of the array, it is better to write $arr[1].

Related topics

Other articles

Comments