in reply to Searching for the remaining smaller values in an array

First, I would agree that Corion's post more accurately describes the algorithm for your desired output for the given input examples.

Got bored, so I came up with the following code:

use strict; use warnings; sub get_output { my ($source,$pair) = @_; my @sorted_src = sort {$a <=> $b} (@{$source}); my @sorted_pr = sort {$a <=> $b} (@{$pair}); my @output; foreach my $sc (@sorted_src) { foreach my $pr (@sorted_pr) { last if ($pr == $sc); push(@output,($sc)) if ($sc < $pr); } } my %temp_hash = map {$_, 1} @output; @output = keys %temp_hash; @output = sort {$a <=> $b} (@output); return @output; } my @source = (1,2,3,4); my @pair = (1,2); my @pair2 = (2,3); my @pair3 = (1,3); my @pair4 = (2,4); my @result; @result = get_output(\@source,\@pair); print "Input list: "; print "@pair\n"; print "Result list: "; print "@result\n\n"; undef @result; @result = get_output(\@source,\@pair2); print "Input list: "; print "@pair2\n"; print "Result list: "; print "@result\n\n"; undef @result; @result = get_output(\@source,\@pair3); print "Input list: "; print "@pair3\n"; print "Result list: "; print "@result\n\n"; undef @result; @result = get_output(\@source,\@pair4); print "Input list: "; print "@pair4\n"; print "Result list: "; print "@result\n\n"; undef @result;

which produces the following output:

Input list: 1 2 Result list: Input list: 2 3 Result list: 1 Input list: 1 3 Result list: 2 Input list: 2 4 Result list: 1 3

Not saying that this is the "best" way, but it does seem to accomplish what you want.