use strict; use warnings; my @array1 = (1,2,3,4); my @array2 = (1,5,3,6,8,2,4); my %hash; use constant kReplace => 3; @hash{@array1} = @array1; my @only2 = grep {! exists $hash{$_}} @array2; # remove the unwanted "value" from array 2 @array2 = grep {$_ != kReplace} @array2; # Substitute a new value in array 1 for (@array1) { next if $_ != kReplace; $_ = shift @only2; last; # Assuming there will only be one value to replace } print "array 1: @array1\n"; print "array 2: @array2\n";