Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: why the array index has to start at 0??

by citromatik (Curate)
on Jun 23, 2009 at 11:53 UTC ( #773978=note: print w/ replies, xml ) Need Help??


in reply to why the array index has to start at 0??

Just for the fun of it...

package base1arrays; use strict; use warnings; sub TIEARRAY { my ($pack,$arr) = @_; bless $arr, $pack; } sub FETCH { my ($arr,$i) = @_; return $arr->[$i-1]; } sub FETCHSIZE { @{$_[0]}; } sub STORE { my ($arr,$i,$v) = @_; $arr->[$i-1] = $v; return; } 1;

Test:

use strict; use warnings; use feature qw/ :5.10 /; use base1arrays; my @orig = qw/1 2 3 4 5/; tie my @arr, 'base1arrays',\@orig or die $!; say for (1..@arr); # "negative" (< 1) index also works say $arr[0]; # Prints 5

citromatik


Comment on Re: why the array index has to start at 0??
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2014-07-13 08:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (248 votes), past polls