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"

I hope this has to do with backups and not homework:

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];
    if ($diff == 1 && $i + 1 < $#codes) {
        ++$nsucc;
    } elsif ($nsucc + 1 >= AT_LEAST_THESE_MANY) {
        $start = $i - $nsucc;
        last;
    } else {
        $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