I tried Google.
I tried Yahoo.
I tried Bing.
None of them had results for the $[ variable. Does anyone know where I can find the documentation for it?
I tried Google.
I tried Yahoo.
I tried Bing.
None of them had results for the $[ variable. Does anyone know where I can find the documentation for it?
perldoc perlvar
on the command line (perldoc should come with perl) or on perldoc.perl.org:
$[ The index of the first element in an array, and of the first
character in a substring. 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. (Mnemonic: [ begins subscripts.)
...
$[ is a scalar containing the first index of all arrays. It's usually 0 since perl arrays are zero-indexed. You can set it to a different value though, like 1, and then all your arrays will start from key 1.
If you have a recent version of perldoc
installed you could run it with the -v option.
perldoc -v '$['
http://perldoc.perl.org/perlvar.html#%24%5b
The index of the first element in an array, and of the first character in a substring. 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 theindex()
andsubstr()
functions. (Mnemonic:[
begins subscripts.)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.Note that, unlike other compile-time directives (such as strict), assignment to
$[
can be seen from outer lexical scopes in the same file. However, you can use local() on it to strictly bind its value to a lexical block.
This variable is deprecated in Perl version 5.12
The main reason you would find the $[
variable, would be if someone used a2p
to transform an Awk script to a Perl script.
For aesthetic reasons you may wish to change the array base
$[
from 1 back to perl's default of 0, but remember to change all array subscripts AND allsubstr()
andindex()
operations to match.
Everyone above has covered it, but I'd like to add that Perldoc is available nicely formatted online at http://perldoc.perl.org/.
Details on all the special variable can be found at http://perldoc.perl.org/perlvar.html.
What are you using it for?