Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (8)
As of 2024-03-28 15:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found