For explicit naming, you'll have to either use my blocks, or hash keys, or use constant. For efficiency and compactness, using constant names defined to indexes in an array is a good option for largish numbers of discrete values:
use constant { THIS => 0, THAT => 1, THEOTHER => 2, ... TEMPL1 => 'N A10 S', }; my @discrete = unpack TEMPL1, read( $file, $size ); print "THIS:", $discrete[ THIS ];
For multi-dim arrays, use subroutines:
sub get2DArray { my( $x, $y, $templ, $templSize, $fh ) = @_; my @array; for my $y ( 0 .. $y - 1 ) { push @array, [ unpack $templ . $x, read( $fh, $templSize * $x +) ]; } return \@array; } my $array2D = get2DArray( 100, 100, 'N', 4, $fh );
You could use nFor or Loops to write a generic multi-dim array reader, but unless you;re going above 3 or 4 dims, separate subs is probably easier. Watch the iteration order; it's can vary.
On my system, using a combination of :perlio on the open & binmode gives me the best reading speed. See Re^2: Perl's poor disk IO performance for details. YMMV.
In reply to Re: Reading binary files - program structure
by BrowserUk
in thread Reading binary files - program structure
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |