Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

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

by citromatik (Curate)
on Jun 23, 2009 at 11:53 UTC ( [id://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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (8)
As of 2024-04-18 13:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found