my $reHeader = '(\b\w+\s*)?' x 10; ## Adjust the repeat value to cover the maximum no of fields
####
#! perl -slw
use strict;
use Data::Dump qw[ pp ];
my $reHeader = '(\b\w+\s*)?' x 10;
my %data;
until( eof( DATA ) ) {
## Read the header line and remove the newline
chomp( my $header = );
## parse the fields using the regex, ignoring undefined fields
my @keys = grep defined, $header =~ $reHeader;
## trim the trailing whitespace from the keys
s[\s*$][] for @keys;
## Use the capture position arrays (@- & @+)
## to work out the field widths and construct a template
my $tmpl = join ' ', map{
defined( $-[$_] )
? do{ my $n = $+[$_] - $-[$_]; "a$n" }
: ()
} 1 .. $#+;
## read and chomp the values line
chomp( my $vals = );
## Extract the value fields using the template
my @vals = unpack $tmpl, $vals;
## trim leading & trailing whitespace
s[^\s*][],s[\s*$][] for @vals;
## Add the key/value pairs to the hash
@data{ @keys } = @vals;
## discard the blank line between the grouped pairs of lines.
;
}
pp \%data; ## display the hash constructed
__DATA__
TRHYST TROFFSETP TROFFSETN AWOFFSET BQOFFSET
2 0 5 3
HIHYST LOHYST OFFSETP OFFSETN BQOFFSETAFR
5 3 0 3
CELLR DIR CAND CS
LUC083A MUTUAL BOTH NO
####
C:\test>junk79
{
AWOFFSET => 5,
BQOFFSET => 3,
BQOFFSETAFR => 3,
CAND => "BOTH",
CELLR => "LUC083A",
CS => "NO",
DIR => "MUTUAL",
HIHYST => 5,
LOHYST => 3,
OFFSETN => "",
OFFSETP => 0,
TRHYST => 2,
TROFFSETN => "",
TROFFSETP => 0,
}