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.


In reply to Re: Searching for the remaining smaller values in an array by dasgar
in thread Searching for the remaining smaller values in an array by neversaint

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.