remiah has asked for the wisdom of the Perl Monks concerning the following question:

Hello monks.

I tried to filter array of hash. I tried three way: with only map, with grep&map, with for loop.
grep&map, for loop works fine but I see strange 'undef' in only map version.

use strict; use warnings; use Data::Dumper; my $data1=[ {key=>'a1',value=>'vala1'}, {key=>'a2',value=>'vala2'}, {key=>'a3',value=>'vala3'}, {key=>'b1',value=>'valb1'}, {key=>'b2',value=>'valb2'}, {key=>'c2',value=>'valc2'}, ]; my %ret; #one map %ret = map{ $_->{key} => $_->{value} if($_->{key} =~/^a\d+/) } @$data1 +; print "map ver\n"; print Dumper \%ret; #grep and map %ret = map{ $_->{key} => $_->{value} } grep{ $_->{key} =~ /^a\d+/ } @$ +data1; print "grep,map ver\n"; print Dumper \%ret; #foreach %ret=(); foreach my $r (@$data1){ $ret{$r->{key}} = $r->{value} if ( $r->{key} =~ /^a\d+/ ); } print "foreach ver\n"; print Dumper \%ret;
output of this.
Odd number of elements in hash assignment at test.pl line 15. map ver $VAR1 = { '' => undef, <====THIS ONE 'a2' => 'vala2', 'a1' => 'vala1', 'a3' => 'vala3' }; grep,map ver $VAR1 = { 'a2' => 'vala2', 'a1' => 'vala1', 'a3' => 'vala3' }; foreach ver $VAR1 = { 'a2' => 'vala2', 'a1' => 'vala1', 'a3' => 'vala3' };
"only map" version causes "Odd number of elements" warning. And I want to ask some advice to understand this warning. What is wrong for only map version?

I thought I once saw similar case at PerlMonk, but I couldn't find one with Super Search.
I am so sorry if this is FAQ.
regards.

Replies are listed 'Best First'.
Re: map with empty item
by Eliya (Vicar) on Mar 15, 2012 at 05:08 UTC

    You have to explicitly specify an empty list (), if you want map to not "return" any items in case the if condition isn't true. Otherwise, map will return undef.

    %ret = map{ $_->{key} =~/^a\d+/ ? ($_->{key} => $_->{value}) : () } @$ +data1;

    The "odd number of elements in hash assignment" in your code resulted from those three undefs corresponding to the non-matching keys b1, b2 and c2. The first two created the hash entry '' => undef, the remaining one created the warning (because hash entries consist of key-value pairs, not a single value).

      You have to explicitly specify an empty list (), if you want map to not "return" any items in case the if condition isn't true. Otherwise, map will return undef.   [...]   The "odd number of elements in hash assignment" in your code resulted from those three undefs corresponding to the non-matching keys... (Emphasis added.)

      The implicit return of a function (such as a map function block) is the value of the last expression evaluated. The statement
          $_->{key} => $_->{value} if($_->{key} =~/^a\d+/)
      will return false (i.e., '', the empty string) if the regex match fails. This is the source of the '' (empty string) key in the output of the map example of the OP. An undef is never generated by the map function of the example; rather, it is generated by the hash constructor trying to pair the '' up with something.

      BTW: The behavior of the incorrect map example code is highly sensitive to the number and order of items in the data array. Try

      my $data1=[ {key=>'c4',value=>'valc4'}, {key=>'a1',value=>'vala1'}, {key=>'a2',value=>'vala2'}, {key=>'a3',value=>'vala3'}, {key=>'b1',value=>'valb1'}, {key=>'b2',value=>'valb2'}, {key=>'c2',value=>'valc2'}, ];

      and notice there is no longer any "Odd number of elements..." warning nor undef value, and there are now pairings like  'vala2' => 'a3' (value and key reversed, mismatched) in the output.

      Thanks, thanks, thanks!

      If I care for unmatched patterns with explicit empty list (), It worked fine!

      Thanks for your explanation.

Re: map with empty item
by jwkrahn (Abbot) on Mar 15, 2012 at 05:12 UTC

    The problem is that the expression $_->{key} => $_->{value} if($_->{key} =~/^a\d+/) will still return something if $_->{key} =~/^a\d+/ doesn't match.    You should try it like this:

    my %ret = map $_->{key} =~ /^a\d/ ? ( $_->{ key } => $_->{ value } ) : + (), @$data1;
      Thanks for reply.

      As Eliya and you points out, I have to care for not match case with empty list. Thanks.