I'm looking for ways to express this Python snippet in Perl:
data = {"A": None, "B": "yes", "C": None}
key_list = [k for k in data if data[k]]
# in this case the same as filter(lambda k: data[k], data) but let's ignore that
So looking at it one way, I just want the keys where the values are None or undef. Looking at it another way, what I want is the concise perl equivalent of a list comprehension with conditional.
I think you want grep
:
#!/usr/bin/env perl
use strict;
use warnings;
my %data = ( A => undef, B => 'yes', C => undef );
my @keys = grep { defined $data{$_} } keys %data;
print "Key: $_\n" for @keys;
I also think that I type too slowly, and that I should reload the page before posting answers. By the way, either a value of 0
or undef
can be a good way to handle null values, but make sure you remember which you're using. A false value and and undefined value aren't the same thing in Perl. To clarify: undef
returns false in a boolean test, but so does 0
. If 0
is a valid value, then you want to explicitly test for definedness, not simply truth. (I mention it because James went for 0
and I went the other way, and you may or may not know if it matters.)
Use grep:
#!/usr/bin/perl
use strict;
use warnings;
my %data = ("A" => 0, "B" => "yes", "C" => 0 );
my @keys = grep { $data{$_} } keys %data;
Grep returns the values from the list on the right-hand side for which the expression in braces evaluates to a true value. As telemachus points out, you want to make sure you understand true/false values in Perl. This question has a good overview of truth in Perl.
You'll likely want a look at map, which applies an expression in braces to each element of a list and returns the result. An example would be:
my @data = ("A" => 0, "B" => 1, "C" => 0 );
my @modified_data = map { $data{$_} + 1 } @data;
print join ' ', @data, "\n";
print join ' ', @modified_data, "\n";
For variation on the theme have a look at autobox (see its implementations autobox::Core and Moose::Autobox )
use autobox::Core;
my %data = ( A => undef, B => 'yes', C => undef );
my $key_list = %data->keys->grep( sub { defined $data{$_} } );
say "Key: $_" for @$key_list;
# => Key: B
Moose::Autobox comes with key/value 'kv' which makes the code DRYer:
my $key_list = %data->kv->grep( sub{ defined $_->[1] } )->map( sub{ $_->[0] } );
Here is a more explicit and even longer version of above:
my $key_list = %data->kv
->grep( sub { my ($k, $v) = @$_; defined $v } )
->map( sub { my ($k, $v) = @$_; $k } );