my @a = (1,2,3,4,5,6); my @b=(1,5,3,6,8,2,4); my %aref = map {$_=>\$_} @a; my $cand; # Candidate value to replace the selected value exists ($aref{$_}) or $cand=$_,last for @b; ${$aref{3}}=$cand; # Assuming you want the value "3" replaced print @a; #--Output -- #128456