Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: Validate array of numbers in sequence

by broquaint (Abbot)
on Sep 08, 2003 at 09:37 UTC ( [id://289728]=note: print w/replies, xml ) Need Help??


in reply to Validate array of numbers in sequence

Here's my lazy attempt
use strict; ## I've lazily pre-sorted these ;) my @ars = ( [ qw/ 1 2 3 4 5 / ], [ qw/ 1 2 3 4 6 / ], [ qw/ 2 3 4 6 7 / ], ); for(@ars) { print "broken list: @$_\n" and next if $_->[0] != 1; my($i, $n) = 0; $n = $_->[$i] while $i <= $#{$_} and $_->[$i] + 1 == $_->[++$i]; print "woo: @$_\n" if $n == $_->[-1]; print "doh: sequence broke at index ${\($i - 1)} (@$_[$i - 1,$i])\n" if $n != $_->[-1]; } __output__ woo: 1 2 3 4 5 doh: sequence broke at index 3 (4 6) broken list: 2 3 4 6 7
So basically it checks the sequence by iterating through the pre-sorted array until there's a gap in the sequence. Then just checks whether the end value is the same as last value seen in the iteration.
HTH

_________
broquaint

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2024-04-25 12:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found