Search code examples
perldictionaryperl-data-structures

Using map in Perl to map only if key exists


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% --


Solution

  • 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;