On Dec 11, 2007, at 9:53 PM, ciwei2103 wrote:

Give a disk sequence know that is incrementing with base 16,  such
as ,

0001
0003
0004
0008
0009
000A
000B
000F
0010
0011

how do I extract the a subset that is have  at least 4 consecutives,
such as  "0008 0009 000A 000B"

Sorry, the previous code missed an edge case, I think this one is correct:

use strict;
use warnings;

use constant AT_LEAST_THESE_MANY => 4;

my @disks = qw(
   0001
   0003
   0004
   0008
   0009
   000A
   000B
   000F
   0010
   0011
);

my @codes = map hex, @disks;

my $start;
my $nsucc = 0;
for (my $i = 0; $i < $#codes; ++$i) {
    my $diff = $codes[$i+1] - $codes[$i];
    ++$nsucc if $diff == 1;
if ($nsucc + 1 >= AT_LEAST_THESE_MANY && ($diff != 1 || $i + 1 == $#codes)) {
        $start = $i - $nsucc;
        ++$start if $diff == 1;
        last;
    } elsif ($diff != 1) {
        $nsucc = 0;
    }
}

print "@disks[$start..($start+$nsucc)]\n" if $start;


-- fxn


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to