开发者

Perl equivalent of Python's list comprehensions with the embedded if statement?

In python, I can do the following to get all the objects in a list with a specific property. In this example I grab the list of id fields of every obj in list objs where obj.id is greater than 100:

ids = [ obj.id for obj in objs 开发者_如何学运维if obj.id > 100]

How would I do the same in perl? I think I want to use map, but I don't know how to conditionally map items from the origin set to the destination set.


The map block can return 0 or more elements for each element in the original list. To omit an element, just return the empty list ():

my @ids = map { $_->id > 100 ? $_->id : () } @objs;

This assumes that the objects in @objs have an id attribute and associated accessor. If you want direct hash access, you can do that too:

my @ids = map { $_->{id} > 100 ? $_->{id} : () } @objs;

Or, you can just combine map and grep:

my @ids = map { $_->id } grep { $_->id > 100 } @objs;

# Or reverse the order to avoid calling $_->id twice:
my @ids = grep { $_ > 100 } map { $_->id } @objs;

I'm not sure which of those would be more efficient, but unless @objs is really big, that's unlikely to matter much.

If the value you're extracting from the object is expensive to calculate, then you can cache the value for the test and return value:

my @vals = map { my $v = $_->expensive_method;  $v > 100 ? $v : () } @objs;


Use grep to return only those items that match the condition. It's like filter in other languages.

grep { condition } @array

For example:

my @nums = (1, 50, 7, 105, 200, 3, 1000);
my @numsover100 = grep { $_ > 100 } @nums;
foreach my $num (@numsover100) {
    print $num . "\n";
}


You could probably get by with combining map and filter, which is essentially what we did in Python before list comprehensions.


Using map and grep together passes over the list twice. Build your own:

sub fancy_filter {
  my ($map_block, $grep_block, @list) = @_;
  my @results;
  foreach my $item (@list) {
    local $_ = $item;
    if ($grep_block->()) {
      push @results, $map_block->();
    }
  }
  return @results;
}

my @ids = fancy_filter(
  sub { $_->{id} },       # map block
  sub { $_->{id} > 100 }, # grep block
  @id_list,
)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜