[Chicago-talk] finding *any* defined elements of a hash

Doug Bell madcityzen at gmail.com
Wed Aug 5 11:32:14 PDT 2015


For some other possibilities, see the List::Util module, which has "any"
and "all".

use List::Util qw( any );
my $has_a_value = any { defined } values %{ $input_hash };

Though it is a core module, those functions have been added more recently,
so unless you have a modern Perl, you may need to update the List::Util
module from CPAN.

Doug Bell
preaction at me.com

On Aug 5, 2015, at 1:05 PM, Andy Lester <andy at petdance.com> wrote:


On Aug 5, 2015, at 1:01 PM, Richard Reina <gatorreina at gmail.com> wrote:

my $input_hash = {

       FName => param('firstname'),
       LName => param('lastname'),
       Email => param('email'),

   };

Can I check to see if any of the hash's element is defined without checking
all the elements as the hash could grow in the future as the fields of the
form grow.


grep through the values and check their count.

my $number_of_defined_values = scalar grep { defined } values
%{$input_hash};

--
Andy Lester => www.petdance.com

_______________________________________________
Chicago-talk mailing list
Chicago-talk at pm.org
http://mail.pm.org/mailman/listinfo/chicago-talk
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.pm.org/pipermail/chicago-talk/attachments/20150805/7929f02c/attachment.html>


More information about the Chicago-talk mailing list