Path: utzoo!attcan!uunet!van-bc!ubc-cs!alberta!oha!tony From: tony@oha.UUCP (Tony Olekshy) Newsgroups: comp.lang.perl Subject: Low Level Array Handling in Perl? Message-ID: <451@oha.UUCP> Date: 27 Sep 90 05:08:24 GMT References: <1990Sep25.012158.20114@uvaarpa.Virginia.EDU> <1990Sep25.041955.12670@iwarp.intel.com> Organization: Olekshy Hoover & Associates Ltd., Edmonton, Alberta, Canada. Lines: 45 In-Reply-To: Message <1990Sep25.041955.12670@iwarp.intel.com> dated 25 Sep 90 04:19:55 GMT 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) ); }