pboin has asked for the wisdom of the Perl Monks concerning the following question:
I'm trying to improve my perl-fu a bit, and I think this is a case where I could map or grep. However, it's not working the way I think it should, and I could use a nudge...
Now, a FAQ is How do I find out if a hash is empty?. That's not exactly what I want to do. I have a hash, I know there are keys out there, but I want to get a quick count of how many of those keys have non-empty values.
Here's a mini-case that I think should work (but doesn't):
#!/usr/bin/perl use strict; use Data::Dumper::Simple; my %a = ( one => 'red', two => '', three => 'blue' ); my @rtn = map {$a{$_} !~ /^$/;} keys(%a); print Dumper(@rtn); print "Keys w/ non-empty values: " . scalar(@rtn) . "\n"; __RESULTS__ @rtn = ( 1, 1, '' ); Keys w/ non-empty values: 3
I think that the matching operator is having its results stored in @rtn. Then, when I get use that return in scalar context, the result isn't quite what I was looking for. Now I have to figure out how many non-empties are in my array instead of my hash, which is pretty much where I started.
Thank you Monasterians...
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Count non-empty hash values
by fishbot_v2 (Chaplain) on Oct 22, 2005 at 17:34 UTC | |
by Joost (Canon) on Oct 22, 2005 at 17:47 UTC | |
by fishbot_v2 (Chaplain) on Oct 22, 2005 at 17:58 UTC | |
by ikegami (Patriarch) on Oct 22, 2005 at 21:23 UTC | |
by Joost (Canon) on Oct 23, 2005 at 00:36 UTC | |
| |
by saskaqueer (Friar) on Oct 22, 2005 at 18:33 UTC | |
by pboin (Deacon) on Oct 22, 2005 at 19:11 UTC | |
by Joost (Canon) on Oct 23, 2005 at 00:46 UTC | |
Re: Count non-empty hash values
by swampyankee (Parson) on Oct 22, 2005 at 17:44 UTC | |
Re: Count non-empty hash values
by Util (Priest) on Oct 22, 2005 at 17:34 UTC |