our @array = 'a' .. 'e'; our @figure_out = 'd' .. 'g'; { my $index = 0; my %h = map {$_=>$index++} @array; while( my($k,$v) = each %h ){ my $n = shift @figure_out or next; $h{$n} = $index++ unless exists $h{$n}; } @array = sort {$h{$a}<=>$h{$b}} keys %h; } #### { my @new = (); my $in = sub { for(@array,@new){ return 1 if $_ eq $_[0] } return 0 }; local $_; while( @array ){ $_ = shift @array; #this is of course pseufo for really figuring out # a new value my $new = shift @figure_out or next; $in->($new) or push @array,$new; } continue { push @new,$_ } @array = @new; }