简体   繁体   中英

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";
}

您可以通过组合mapfilter来实现 ,这基本上就是我们在列表推导之前在Python中所做的。

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,
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM