Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: get next higher number

by AnomalousMonk (Archbishop)
on Jun 11, 2018 at 19:34 UTC ( [id://1216430]=note: print w/replies, xml ) Need Help??


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

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:  <%-{-{-{-<

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1216430]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (2)
As of 2024-04-26 04:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found