use Data::Dumper; my %hash; my @n = map { /(Character|ASCII)\s*?=\s*?(\S+)/; $1 ? [$1,$2] : () } ; while (my ($a, $b) = splice @n, 0, 2) { if ($a->[0] eq 'ASCII') { $hash{$b->[1]} = $a->[1]; } else { $hash{$a->[1]} = $b->[1]; } } print Dumper(\%hash); __DATA__ -------------------- ASCII=1 Character=2 Decimal=970 Hexidecimal=3CA Entity name= Decription=i with an umlaut -------------------- Character=4 ASCII=3 Decimal=39 Hexidecimal=27 Entity name= Decription=quote --------------------- ASCII=< Character=< Decimal=60 Hexidecimal=3C Entity name=lt Decription=less than