Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re^2: how internally $#array is working in Perl

by sflitman (Hermit)
on Aug 13, 2010 at 06:00 UTC ( [id://854839]=note: print w/replies, xml ) Need Help??


in reply to Re: how internally $#array is working in Perl
in thread how internally $#array is working in Perl

From perlvar:
$[ 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 ma +ke Perl behave more like awk (or Fortran) when subscripting and when evaluatin +g the index() and substr() functions. (Mnemonic: [ begins subscripts.) As of release 5 of Perl, assignment to $[ is treated as a compiler dir +ective, and cannot influence the behavior of any other file. (That's why you can o +nly assign compile-time constants to it.) Its use is deprecated, and by default w +ill trigger a warning. Note that, unlike other compile-time directives (such as strict), assi +gnment to $[ can be seen from outer lexical scopes in the same file. However, yo +u can use local() on it to strictly bind its value to a lexical block.
I suspect you can assign 1 to it, but other values are silently ignored. I tried your code with $[=1, and lo and behold:
perl -le '@arr=qw(10 1 2 3 4);$[=1;print $arr[$_],"\t",$_ for 0 .. $#a +rr;' 10 0 10 1 1 2 2 3 3 4 4 5
(shrug)

SSF

Replies are listed 'Best First'.
Re^3: how internally $#array is working in Perl
by DrHyde (Prior) on Aug 13, 2010 at 09:59 UTC
    I suspect you can assign 1 to it, but other values are silently ignored

    You suspect incorrectly.

    $ perl -e '@array=(qw(ant bat cat)); $[ = 94; print $array[95]."\n"' bat $ perl -e '@array=(qw(ant bat cat)); $[ = -9; print $array[-8]."\n"' bat

      You're not even limited to positive numbers.

      $ perl -wle'$[ = -3; $_ = "abcdef"; print substr($_,0,1)' Use of assignment to $[ is deprecated at -e line 1. d

      You could get weird constructs that caused Perl to crash:

      $ perl -we'$[ = 3; $_ = "abc"; my $x = substr($_,2,1)' panic: sv_setpvn called with negative strlen at -e line 1.

      I fixed it in 5.10.1 or 5.12.0 when fixing another bug:

      $ perl -we'$[ = 3; $_ = "abc"; my $x = substr($_,2,1)' Use of assignment to $[ is deprecated at -e line 1. substr outside of string at -e line 1.

      Keep in mind $[ is slated to be removed from Perl.

        You're not even limited to positive numbers.
        yes, as I demonstrated with my example that used -9 :-)
        Keep in mind $[ is slated to be removed from Perl.
        has a2p been considered when making this change?
Re^3: how internally $#array is working in Perl
by nvivek (Vicar) on Aug 13, 2010 at 08:50 UTC

    How internally array holds the value like this way.I have checked the code by using Dumper function in Data::Dumper module.
    In that, it printed only 10,1,2,3,4 but when I loop through the array.It printed 6 values as 10,10,1,2,3,4.Explain me how it works?
    Thanks in advance

      If you set $[ to 1 you tell perl that array index 0 doesn't exist. Now ask yourself: What should perl do if you then access or print $a[0]? What's the value of an array index that doesn't exist? The answer is: Anything

      If you write  print 1/0; you don't expect a meaningful answer, right? Similarly you don't get a meaningful value in $a[0] if you tell perl that $a[0] doesn't exist

      The correct way to handle access to index 0 would be to print an error message or warning. But that might break some really old scripts. So perl seems to print the same value that is in index 1, the first "legal" index.

      And a word of advice: Don't change $[. And if you want to loop through an array, foreach (@a) {} will get you any value, no matter at what index the array begins and ends

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://854839]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (2)
As of 2024-04-24 22:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found