sub read_dict{ my $file = shift; my %dict; open( my $fh, "<:encoding(utf5)", $file ); while( ) { chomp; ## no need to chomp twice my ($p1, $p2) = split /\t/; push( @{ $dict{ $p1 } }, $p2 ); } close $fh; return \%dict; ## main space saving change; return a ref to the hash } ... my $dict = read_dict( $dict_name ); ... for my $next_phrase ( @{ $dict->{ $key } } ){ ... }