my $stn; # current stn hash being constructed ... while () { if (/^H/ || /^reclin/i) { if ($stn) { ...save $stn...; $stn = undef; # reset stn hash } } if (/^H/ ... /[<\[]/) { ... same code ... } next if (/^reclin/i); ... parse new $stn hash values ... $stn->{$s} = [ $x, $y, $z ]; } if ($stn) { ...save $stn... }