in reply to retrieve next avaiable element in sorted hash
But at second glance I realize that $elem+1 may not exist either in which case you should just failout like so:#!/usr/bin/perl -w use strict; my %hash = ('1' => 'cat', '3'=> 'mouse', '5'=>'whale' ); my $elem = 2; if (defined($hash{2})) {print $hash{2}} else { print $hash{$elem+1}}; 1;
#!/usr/bin/perl -w use strict; my %hash = ('1' => 'cat', '3'=> 'mouse', '5'=>'whale' ); my $elem = 2; if (defined($hash{$elem})) { print $hash{$elem} } elsif (defined $hash{$elem + 1} ) { print $hash{$elem+1} } else { print 'ERROR!!!!!!'; } 1;
Celebrate Intellectual Diversity
|
|---|