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

The below script ignores two fields called 'Name' and 'Phone'. But I dont understand how it is working. I know 'map' executes a code block but need more assistance on what is inside this map block. Can someone please explain it?
next if exists { map {$_ => undef } qw(Name Phone) } -> { $field };

Replies are listed 'Best First'.
Re: Interpretation of a line needed.
by Abigail-II (Bishop) on Sep 23, 2003 at 16:05 UTC
    It's a difficult, and inefficient way of writing:
    next if $field eq "Name" or $field eq "Phone";

    Abigail

      Good Lord, ++ parent message. Abigail's version is so much easier to read.

      -------------------------------------
      Nothing is too wonderful to be true
      -- Michael Faraday

Re: Interpretation of a line needed.
by broquaint (Abbot) on Sep 23, 2003 at 15:46 UTC
    It's basically creating a temporary hash and testing if $field exists in the hash. So
    map { $_ => undef } qw(Name Phone)
    Create a list of key value pairs from the given list where the keys are each element of the list and values are undef
    { map { $_ => undef } qw(Name Phone) }
    Populate an anonymous hash with the list that was created from the map
    exists { map { $_ => undef } qw(Name Phone) } -> { $field };
    Check for the existence of $field in the anonymous hash reference.

    Perl Idioms Explained - keys %{{map{$_=>1}@list}} may also be of help.
    HTH

    _________
    broquaint

      Thanks all!
Re: Interpretation of a line needed.
by zby (Vicar) on Sep 23, 2003 at 15:54 UTC
    The result of this code: {map {$_ => undef } qw(Name Phone)} is this structure:
    { "Name" => undef, "Phone" => undef}
    Then it is checked if $field is a key in this structure.

    More precisely what I called here a structure is a pointer to an anonymous hash. Thats why the exists function can be used.

    Hope that explains how it works. But I what I can't explain is why is it so obfuscated.