in reply to Unpack Many Fields
If you only need 34 of 170 fields, you can use x[nnn] to skip the bytes between the fields you want. That should reduce the unweildy length of your template considerably.
And if the template is still uncomfortably long:
my $fmt = join '', qw[ x[10] a5 x[22] a10 x[4] a3 a2 ... ];
You can even turn that into an opportunity to document:
my $fmt = join '', grep !m[^#], qw[ x[10] a5 #code x[22] a10 #thingummy x[4] a3 a2 #doodah&whatsit ... ];
As for the naming of the fields, rather than using strings of "FIELDnn" in a hash, why not use an array?
Eg.
my $fmt = "x[10]a5x[22]a10x[4]a3a2..."; my %hash; while( <$fh> ) { my( $code, @fields ) = unpack $fmt, $line; $hash->{$code} = \@fields; } ... for my $code ( keys %hash ) { print $hash->{ $code }[ $_ ] for 0 .. 33; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Unpack Many Fields
by ikegami (Patriarch) on Feb 16, 2010 at 01:53 UTC | |
by shoness (Friar) on Feb 16, 2010 at 02:23 UTC | |
by ikegami (Patriarch) on Feb 16, 2010 at 02:47 UTC | |
by shoness (Friar) on Feb 16, 2010 at 02:54 UTC | |
by BrowserUk (Patriarch) on Feb 16, 2010 at 03:01 UTC | |
by ikegami (Patriarch) on Feb 16, 2010 at 05:09 UTC | |
by BrowserUk (Patriarch) on Feb 16, 2010 at 05:48 UTC |