It seems to "work" for me.
#!/usr/local/bin/perl -w use strict; sub find_holes { my @list = @{ shift() }; @list = sort { $a <=> $b } @list; my $low = $list[0]; my $high = $list[-1]; my %isthere = map { $_ => 0 } ($low..$high); $isthere{$_} = "yes" for @list; my @vacancies = grep { not $isthere{$_} } sort keys %isthere; return \@vacancies; } my @foo = qw(1 2 5 6 9); my $vac = find_holes(\@foo); print "@$vac\n"; __END__ $ ./holes.pl 3 4 7 8
In reply to Re: Finding missing elements in a sequence (code)
by kwoff
in thread Finding missing elements in a sequence (code)
by deprecated
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |