use strict; open( FILE, '<', "whatever.filename' ) or die "whatever... $!\n"; my $container; my ( %SIR, %SFR ); while (){ chomp; if ( /SPECint_base/ ) { $container = \%SIR; } elsif ( /SPECfp_base/ ) { $container = \%SFR ; } elsif ( $container ) { $container->{$_} = undef; # no need to repeat hash key as hash value } }