sub schlep($$$) { my( $map_ref, $from_ref, $to_ref ) = ( shift, shift, shift ); for( keys %{ $from_ref } ) { $to_ref->{ $map_ref->{$_} } = $from_ref->{$_} if( exists $map_ref->{$_} && exists $to_ref->{ $map_ref->{$_} } ) } } schlep(\%map_foo_to_bar, \%foo, \%bar); use Data::Dumper; print Dumper \%bar;