3
votes

I have a data structure that has been defined like this:

my @columns = (
  {
    db => 'location',
    dt => 'location',
    search => 'location',
  },
  {
    db => 'name',
    dt => 'name',
    search => 'name',
  },
  {
    db => 'iqs',
    dt => 'iqs',
  },
);

I am mapping the values of search like so:

@where = map +{ $_->{search} => { like => "\%$global_search\%" } }, @columns;

If the key search does not exist, I end up with a data structure that has a bunch of these:

{
    '' => {
        'like' => '%pas%'
    }
},

And that completely screws up what I am trying to do. So, I am wondering, since map isn't technically a loop, how can I skip when the key search does not exist?

EDIT:

Someone below had asked about performance differences between map vs loop, and this piqued my curiosity, so I decided to test it myself. Here are the results:

The Code

#!/usr/bin/env perl
use strict;
use warnings;
use Benchmark qw/cmpthese timethese/;

my $global_search = 'test';


my @columns;
for ( my $i = 0; $i <= 100000; $i++ ) {
    my $hash = {
        db => "test$i",
        dt => "test$i",
        search => "test$i"
    };
    push @columns, $hash;
}

timethese(-30, {
      mapBuild => sub {
        my @where = map {
          exists ($_->{search})
            ? +{ $_->{search} => { like => "\%$global_search\%" } }
            : ()
        } @columns;
      },
      forBuild => sub {
        my @where;
        foreach (@columns) {
          if (exists $_->{search}) {
            push @where, +{ $_->{search} => { like => "\%$global_search\%" } } ;
          }
        }
      },
});

cmpthese(-30, {
      mapBuild => sub {
        my @where = map {
          exists ($_->{search})
            ? +{ $_->{search} => { like => "\%$global_search\%" } }
            : ()
        } @columns;
      },
      forBuild => sub {
        my @where;
        foreach (@columns) {
          if (exists $_->{search}) {
            push @where, +{ $_->{search} => { like => "\%$global_search\%" } } ;
          }
        }
      },
});

The Results

Benchmark: running forBuild, mapBuild for at least 30 CPU seconds...
forBuild: 32 wallclock secs (31.66 usr + 0.00 sys = 31.66 CPU) @ 980.35/s (n=31038) mapBuild: 31 wallclock secs (31.58 usr + 0.00 sys = 31.58 CPU) @ 994.21/s (n=31397) Rate forBuild mapBuild forBuild 978/s -- -2% mapBuild 993/s 2% --

3
I answered my own question mere seconds after posting this. I will leave it here in case anyone else is looking for the answer: @where = map +{ $_->{search} => { like => "\%$global_search\%" } }, grep { exists $_->{search} } @columns; However, considering Perl and TMTOWTDI, I'd be interested in other ways people might accomplish this.Franz Kafka
grep before map, or map returning empty list ()mpapec

3 Answers

4
votes

The block passed to map gets called in list context, so it can return an empty list:

@where = map {
   exists ($_->{search})
      ? +{ $_->{search} => { like => "\%$global_search\%" } }
      : ()
} @columns;

Or you could just grep first:

@where =
   map +{ $_->{search} => { like => "\%$global_search\%" } },
   grep exists($_->{search}),
   @columns;
1
votes
@where = map {
    $_->{search} => { like => "\%$global_search\%" }
} grep {
    defined $_->{search}
} @columns;
1
votes

Could always try turning it into a loop that reads more like English.

my @where;

for (@columns) {
    if ( exists $_->{search} ) {
        push @where, +{ $_->{search} => { like => "\%$global_search\%" } };
    }
}

Does anyone know if there's a huge performance hit from doing what I just wrote, or it roughly the same speed as map?