The index of the first element in a Perl array

In the Perl programming language, as in many other programming languages, the first element in the array has an index 0. But there was a possibility (which is highly not recommended to use) to make the index of the first element was 1 (or other number).

Here is an example code:

▶ Run
#!/usr/bin/perl

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

print $arr[1];

The program will display the text two. We refer to array element with index 1 and this second element of the array.

  • index 0 is the first element of the array, in our example, the string 'one'
  • index 1 is the second element of the array, in our example, the string 'two'
  • index 2 — is the third element of the array, in our example, the string 'three'

For learners of programming is unusual, but this is standard. 0 is the index of the first element of the array in almost all programming languages.

Variable $[

The creators of the Perl programming language wanted to make the language such that the programmer decided for himself what he wants to use the language (the principle of TIMTOWTDI), therefore, language was a way to change the index of the first element. If someone is uncomfortable use 0 as the index of the first element, you could say what is the index of the first element will be 1.

Here is an example code:

#!/usr/bin/perl

$[ = 1;

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

print $arr[1];

Set the value of the custom variable per unit $[ = 1; — say this now the index of the first element will be 1, not 0. The program will display the text one.

But it quickly became clear that this opportunity does not simplify, but rather complicates the work ID. Here you open the program and before it to change something you first need to understand how is the work with the array.

So Perl 5.12 using variable $[ led to the appearance warning: Use of assignment to $[ is deprecated at script.pl line 3..

The Perl Version 5.12, 5.14, 5.16, 5.18, 5.20, 5.22. 5.24, 5.26 issued exactly this warning. 5.28 was to issue a warning Use of assignment to $[ is deprecated, and will be fatal in Perl 5.30 at script.pl line 3., and Perl 5.30 stopped giving the ability to change the index of the first element. When you try to do it the program fails with an error text Assigning non-zero to $[ is no longer possible at script.pl line 3..

So in recent versions of Perl the first element of the array always has the index 0. In Perl prior to this version have the opportunity to make another number by the index of the first element, but the best of this opportunity never use.

Official documentation

Here's a snippet of the output perldoc perlvar Pro variable $[:

    $[      This variable stores the index of the first element in an array,
            and of the first character in a substring. The default is 0, but
            you could theoretically set it to 1 to make Perl behave more
            like awk (or Fortran) when subscripting and when evaluating the
            index() and substr() functions.

            As of release 5 of Perl, assignment to $[ is treated as a
            compiler directive, and cannot influence the behavior of any
            other file. (That's why you can only assign compile-time
            constants to it.) Its use is highly discouraged.

            Prior to Perl v5.10.0, assignment to $[ could be seen from outer
            lexical scopes in the same file, unlike other compile-time
            directives (such as strict). Using local() on it would bind its
            value strictly to a lexical block. Now it is always lexically
            scoped.

            As of Perl v5.16.0, it is implemented by the arybase module.

            As of Perl v5.30.0, or under "use v5.16", or "no feature
            "array_base"", $[ no longer has any effect, and always contains
            0. Assigning 0 to it is permitted, but any other value will
            produce an error.

            Mnemonic: [ begins subscripts.

            Deprecated in Perl v5.12.0.

Other articles

Comments