in reply to get next higher number
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):
I leave further testing to you :)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
(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: <%-{-{-{-<
|
|---|