in reply to Reading binary files - program structure
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Reading binary files - program structure
by ikegami (Patriarch) on May 20, 2010 at 15:51 UTC | |
by BrowserUk (Patriarch) on May 20, 2010 at 16:01 UTC | |
by Anonymous Monk on May 24, 2010 at 17:16 UTC | |
by BrowserUk (Patriarch) on May 24, 2010 at 20:38 UTC | |
by Anonymous Monk on May 25, 2010 at 16:47 UTC | |
|