Can I do it some how with regex?
As hippo has alluded, a regex approach would seem far from ideal. Howsoever, the following seems to work, but requires Perl version 5.10+ for the (?(condition)yes-pattern|no-pattern) construct (which embeds the experimental (?{ CODE }) feature; see Extended Patterns in perlre):
c:\@Work\Perl>perl -wMstrict -le
"use 5.010;
;;
use Test::More 'no_plan';
use Test::NoWarnings;
;;
my @values = qw(
11673326 11673329 11673325 11673330 11673321 11673335
);
;;
my $sorted_str = join ' ', sort { $a <=> $b } @values;
;;
sub next_higher {
local our ($from) = @_;
;;
return $1 if $sorted_str =~ m{
\b (\d+) \b
(?(?{ $from >= $^N }) (*FAIL))
}xms;
;;
return;
}
;;
is next_higher(11673326), 11673329;
is next_higher(11673335), undef;
is next_higher(11673321), 11673325;
done_testing;
"
ok 1
ok 2
ok 3
1..3
ok 4 - no warnings
1..4
I leave further testing to you :)
(NB: From, I believe, Perl version 5.18 on, the
local our ($from) = @_;
statement in the next_higher() function can be replaced with a more familiar lexical
my ($from) = @_;
statement and the regex will work properly; can't test this ATM.)
Update: When discussing Perl version dependency in the first paragraph, I should also have mentioned that (*FAIL) was introduced with 5.10 (see Special Backtracking Control Verbs in perlre). However, the effect of (*FAIL) is exactly duplicated by (?!) in pre-5.10 regexen, so (?(condition)yes-pattern|no-pattern) remains the critical problem.
Give a man a fish: <%-{-{-{-<
-
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.