neversaint has asked for the wisdom of the Perl Monks concerning the following question:
Dear Masters,
Given the following array:
Given the following array:
and input pairmy @source = (1, 2, 3, 4);
We would like to find keys in `@source` where the values is smaller than members of any given pairs. So the desired output for each of above pairs are:my @pair = (1,2); my @pair2 = (2,3); my @pair3 = (1,3); my @pair4 = (2,4);
What's the right algorithm to do that. The following is my code but fail:@pair -> [] @pair2 -> [1] @pair3 -> [2] @pair4 -> [1,3]
sub get_output { my ($source,$pair) = @_; my @output = (); my %done = (); foreach my $pr (@{$pair}){ foreach my $sc (@{$source}){ next if ($pr <= $kn || $done{$sc}); push @output,$sc; $done{$sc} = 1; } } use Data::Dumper; print Dumper \@output; return @output; }
---
neversaint and everlastingly indebted.......
neversaint and everlastingly indebted.......
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Searching for the remaining Smaller values in an array
by Corion (Patriarch) on Jun 20, 2012 at 13:24 UTC | |
Re: Searching for the remaining smaller values in an array
by dasgar (Priest) on Jun 20, 2012 at 16:27 UTC | |
Re: Searching for the remaining smaller values in an array
by Anonymous Monk on Jun 20, 2012 at 13:23 UTC | |
Re: Searching for the remaining smaller values in an array
by sundialsvc4 (Abbot) on Jun 20, 2012 at 13:55 UTC |
Back to
Seekers of Perl Wisdom