in reply to Two dimentional array
Two dimensional array of integers? Smells like C... :-)
package Array2D; use strict; use Carp; use constant sizeof_int => length pack 'i', 1337; sub new { my $class = shift; @_ == 2 or croak "must call ${class}::new with x, y dimensions"; my ($dx, $dy) = @_; $dx > 0 && $dy > 0 or croak "${class}::new : illegal x, y dimensions"; bless { dx => $dx, dy => $dy, buf => "\0" x ($dx * $dy * sizeof_int) }, $class; } sub get { my $ary = shift; my $class = ref $ary; @_ == 2 or croak "must call ${class}::get with x, y positions"; my ($x, $y) = @_; my ($dx, $dy) = @{$ary}{qw/dx dy/}; $x >= 0 && $y >= 0 && $x < $dx && $y < $dy or croak "${class}::get : subscripts out of range"; unpack 'i', substr ( $ary->{buf}, ($x + $y * $dx) * sizeof_int, sizeof_int ); } sub set { my $ary = shift; my $class = ref $ary; @_ == 3 or croak "must call ${class}::get with x, y positions and new value"; my ($x, $y, $val) = @_; my ($dx, $dy) = @{$ary}{qw/dx dy/}; $x >= 0 && $y >= 0 && $x < $dx && $y < $dy or croak "${class}::set : subscripts out of range"; substr ( $ary->{buf}, ($x + $y * $dx) * sizeof_int, sizeof_int, pack 'i', $val ); (); } 1;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Two dimentional array
by algonquin (Sexton) on Sep 11, 2004 at 21:59 UTC | |
by calin (Deacon) on Sep 12, 2004 at 00:19 UTC |