tony@oha.UUCP (Tony Olekshy) (09/27/90)
Here is an idea for handling arrays in Perl that I have not yet heard of.
Comments?
$array = &MakeArray("l", 14, 12); # Type, Rows, Columns.
for ($r = 0; $r < 14; $r += 1) {
for ($c = 0; $c < 12; $c += 1) {
&PutArray(*array, $r, $c, 100 * ($r+1) + $c + 1);
}
}
for ($r = 0; $r < 14; $r += 1) {
for ($c = 0; $c < 12; $c += 1) {
printf " %4d", &GetArray(*array, $r, $c);
}
print "\n";
}
sub MakeArray
{
local($type, $rows, $cols) = ($_[0], $_[1], $_[2]);
local($tmpl8) = "aLL"; # Type, Rows, Cols.
$ArrayHeader = length(pack($tmpl8, " ", 0, 0));
$tmpl8 .= $type x ($rows * $cols);
return pack($tmpl8, $type, $rows, $cols);
}
sub PutArray
{
local(*array, $row, $col, $val) = ($_[0], $_[1], $_[2], $_[3]);
local($type, $rows, $cols) = unpack("aLL", $array);
local($sizeof) = length(pack($type, 0));
substr($array, $ArrayHeader + ($row * $cols + $col) * $sizeof, $sizeof)
= pack($type, $val);
}
sub GetArray
{
local(*array, $row, $col) = ($_[0], $_[1], $_[2]);
local($type, $rows, $cols) = unpack("aLL", $array);
local($sizeof) = length(pack($type, 0));
return unpack($type,
substr($array, $ArrayHeader + ($row * $cols + $col) * $sizeof, $sizeof)
);
}