find the missing sequence in hash perl

Dear Perl's Users,

Could anyone help me how to solve my problem. I have data with details below.

TTY NAME SEQUENCES
U-0 UNIX 0
U-1 UNIX 1
U-2 UNIX 2 <-- From 2 jump to 5
U-5 UNIX 5
U-6 UNIX 6 <-- From 6 jump to 20
U-20 UNIX 20
U-21 UNIX 21
U-22 UNIX 22

I want the result below.

U-0&&-2 UNIX 0
U-5&&-6 UNIX 5
U-20&&-22 UNIX 20

Thank you very much for your answer

my $start;
my $last;
while (<>) {
if ( /^U-\d+ UNIX (\d+)$/) {
   if (!defined $start) {
        $start = $1;
        $last = $1;
        next;
   }
   elsif (!defined $last || $1 > $last + 1) {
       print "U-${start}&&-${last} UNIX ${start}\n";
       $start = $last = $1;
   }
   else {
       $last = $1;
   }
}
}
print "U-${start}&&-${last} UNIX ${start}\n";
1 Like

Hi Otheus,

Thanks alot for your answer.