Hello John,
On Mon, 04 Jun 2012 14:19:27 -0700
"John W. Krahn" <[email protected]> wrote:
> Chris Stinemetz wrote:
> > I have a subroutine that I want to "return 1" only if the value of
> > %{$href->{$_[0]}} is equal to 'ND' for the whole 24 occurences.
>
>
> One way to do it:
>
> sub site_offAir {
> return values %{ $href->{ $_[ 0 ] } } == grep( $_ eq 'ND', values
> %{ $href->{ $_[ 0 ] } } ) ? 1 : '';
> }
>
I see several problems with your code:
1. It's quite hard to understand the logic of it.
2. It won't stop when it encounter the first "ND" value.
3. You have the values % { $href->{ $_[0] } } gob twice (a duplicate
expression).
4. You've used $_[0] which is a positional parameter, see:
http://perl-begin.org/tutorials/bad-elements/#subroutine-arguments
5. The grep does not uses braces for its predicate/block which is harder to
read.
6. You will return a true value (a list of length 1) when the function is
called in list context.
----
I prefer the suggestions in the previous sub-thread with the looping and
returning. Other ones can be written in a good manner using List::MoreUtils'
any/all/none/notall , and the smart match operator.
While we are discussing bad solutions, another option is to do:
# Bad code - don't use
sub site_offAir
{
my ($hash_ref) = @_;
my @v = keys(reverse(%$hash_ref));
return ((@v == 1) and ($v[0] eq 'ND'));
}
But please don't use it.
Regards,
Shlomi Fish
--
-----------------------------------------------------------------
Shlomi Fish http://www.shlomifish.org/
Best Introductory Programming Language - http://shlom.in/intro-lang
<danderson> “We are NO LONGER the knights who say ‘BitKeeper’. We are now
the knights who say ‘git, git, git, cogito — Linus!’.”
Please reply to list if it's a mailing list post - http://shlom.in/reply .
--
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/