--- "Beau E. Cox" <[EMAIL PROTECTED]> wrote:
> Hi -
>
> Please try:
>
> use strict;
>
> my @list = (1, 2, 3, 4, 10, 14, 15, 16, 20, 34, 35, 36,);
> my %conseq; # temp hash
> $conseq{$_} = $_ for (@list);
> for (keys %conseq) {delete $conseq{$_} unless $conseq{$_-1} or
> $conseq{$_+1}}
> my @conseq; # result (note: remember scalars, arrays, hashes
> # have their own namespaces, so same "name" is ok)
> push @conseq, $_ for (sort {$a <=> $b} keys %conseq);
> print "$_\n" for (@conseq);
>
> Maybe people will yell at me because it's such a kludge, but it
> works:
> 1) copy to hash with the key the same as value,
> 2) pass hash deleting keys that don't have +1 or -1 neighbors,
> 3) numerically sort hash and populate result array.
The above will work but not if the values are not unique. I thought
about a hash but decided to make the answer look like what his teacher
may expect from him/her ;-). Is there a policy that homework questions
are not to be answered.
I have changed the answer a little so that you will have to do
something for it, if anyone wants the answer without the has email me
off list.
#!perl
use warnings;
use strict;
my @array = (1, 2, 3, 4, 10, 14, 15, 16, 20, 34, 35, 36);
my $count = 0;
for ($count = 0 ; $count < $#array ; $count++) {
my $one = $array[$count] + 1;
my $two = $array[$count + 1];
my $three = $array[$count - 1] ;
if ($one == $two ){
print "$array[$count]\n";
}else {
if ($array[$count] == $array[$count - 1] + 1) {
print "$array[$count]\n";
}
}
if ($count == $#array - 1){
if ($array[$count] + 1 == $array[$count - 1] ){
print "$array[$count]";
}
}
}
H
__________________________________________________
Do You Yahoo!?
LAUNCH - Your Yahoo! Music Experience
http://launch.yahoo.com
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]