Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Explain $[

Status
Not open for further replies.

raklet

MIS
Aug 19, 2003
370
US
What is the special variable $[.
 
$[ Compliance

Short Name $[

Scope localize

Definition

This variable, which is usually set to a value of 0, represents the index of the first element in any array. Programmers who are used to using 1 as the index of the first element of an array could change the value of this variable to suit their preference.
Example

$[ = 1;
$_ = "AlphaBetaGamma";
$tmp = index($_,"Beta");
print "Beta located at: $tmp\n";
$[ = 0;
$_ = "AlphaBetaGamma";
$tmp = index($_,"Beta");
print "Beta located at: $tmp\n";

 
In the "books", they basically say "You can change this, but don't", my guess is its gonna affect some of Perl's internals.

Thought about it when coming from VB(1 based vs 0 based arrays), but they seemed so nice about it, I didn't want to upset them, ;-)

Paul
------------------------------------
Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
Seems odd to even concidering changing the index default, once armed with the knowledge that PERL arrays start at zero, $#array gives you last index and $count = @array; gives number of index's in an array, you don't need to change anything , do you ?

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
It's just something there for programmrs to use if they need to. There probably never is a real need to use it though.
 
changing $[ will upset the intended use of the index function if altered for example

Paul
------------------------------------
Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
It's fun for obfuscation too ;-)
Code:
my @array = qw/one two three four/;

print $array[$[],"\n";
 
It's fun for obfuscation too
- can you say that in English please ishnid ;-)

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
so it's good to do people's heads in when tring to read your code, is that what you mean - lol

[3eyes]

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top