Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

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

by nvivek (Vicar)
on Aug 13, 2010 at 08:50 UTC ( #854859=note: print w/replies, xml ) Need Help??

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

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

  • Comment on Re^3: how internally $#array is working in Perl

Replies are listed 'Best First'.
Re^4: how internally $#array is working in Perl
by jethro (Monsignor) on Aug 13, 2010 at 09:34 UTC
    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?

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

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (3)
As of 2023-02-05 20:42 GMT
Find Nodes?
    Voting Booth?
    I prefer not to run the latest version of Perl because:

    Results (32 votes). Check out past polls.