Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Strange grep construct

by mzvk (Novice)
on Apr 12, 2019 at 11:55 UTC ( #1232484=perlquestion: print w/replies, xml ) Need Help??
mzvk has asked for the wisdom of the Perl Monks concerning the following question:

Hello keepers of knowledge, I have humble request, to shed some light and guide fellow monk apprentice. I stumble across some piece of Perl code, which I cannot really wrap my head around. It suppose to create an intersection of two arrays (with lower casing all characters).
my @intersection = grep { defined } @{ { map { lc ,=> $_ } @a } } { map { lc } @b };
I've run it through deparse to see what's not seen by naked eye.
$ perl -MO=Deparse -e 'my @intersection = grep { defined } @{ { map { lc ,=> $_ } @a } } { map { lc } @b }; ' my(@intersection) = grep({defined $_;} @{+{map({lc $_, $_;} @a)};}{map + {lc $_;} @b}); -e syntax OK

So this explained me that ',=> $_' is actually just ', $_' but is there a reason for using a fat comma, or is it "Winking fat comma" (still dunno why it's used)?
Why 1st map is enclosed by @{} is it dereferencing output of map?

Probably it's something easy. I just want to understand the mechanics and maybe learn something new.


thanks
m.

Replies are listed 'Best First'.
Re: Strange grep construct
by choroba (Bishop) on Apr 12, 2019 at 12:16 UTC
    Fat comma would have interpreted lc as a string. Normal comma would have been less pretty.

    The whole dereferencing is a bit complex, but it basically uses the @hash{key1, key2} slice syntax. I.e., the first map maps lower case strings from @a to their originals, this is inserted into an anonymous hash, and only the values corresponding to lowercased elements of @b are returned to the outer grep that selects the defined ones.

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
      Ah, now I see. Thanks a lot!
Re: Strange grep construct
by LanX (Archbishop) on Apr 12, 2019 at 12:29 UTC
    The comma in ,=> is a NOP.

    • The fat comma is just a comma (sic).
    • multiple "empty" commas are ignored (that's why we have 'undef')
    DB<8> x ["a",=>"b"] 0 ARRAY(0x54e32f0) 0 'a' 1 'b' DB<9> x ["a",,"b"] 0 ARRAY(0x54e2cd8) 0 'a' 1 'b'

    Like choroba already explained it's a syntactic trick to avoid lc to be stringified by =>

    edit

    so whats happening is a construction of hash pairs, where the keys are normalized to lowercase. The fat-comma is meant to clarify this pair nature.

    DB<12> x map {lc,$_} "A".."C" 0 'a' 1 'A' 2 'b' 3 'B' 4 'c' 5 'C'

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

Re: Strange grep construct
by tybalt89 (Vicar) on Apr 12, 2019 at 12:18 UTC

    Here's a guess.

    my @lowercase_b = map lc, @b; my %lowercase_a = map { lc, $_ } @a; my @intersection_and_undefs = @lowercase_a{@lowercase_b}; my @intersection = grep defined, @intersection_and_undefs;

    "Why 1st map is enclosed by @{}" => It's a hash slice.

Re: Strange grep construct
by mzvk (Novice) on Apr 12, 2019 at 12:39 UTC
    Thanks all of you! If there was something I was too "fresh" to understand, other replies actually clarified that.
      > I was too "fresh" to understand

      No worries, I'm here for over 10 years with thousands of posts and I can't remember seeing a hash slice on an anonymous hash constructed from a map. (good to know it's possible, though)

      also the ,=> is highly idiomatic.

      I have trouble seeing a justification for this degree of golfing, other than probably (micro) optimization.

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

        I have trouble seeing a justification for this degree of golfing,

        Perl golf is the art of using as few characters as possible so this doesn't really qualify.

        With unnecessary characters removed:

        my@i=grep defined,@{{map{lc,$_}@a}}{map{lc}@b};

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1232484]
Front-paged by haukex
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (5)
As of 2019-04-20 10:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I am most likely to install a new module from CPAN if:
















    Results (108 votes). Check out past polls.

    Notices?
    • (Sep 10, 2018 at 22:53 UTC) Welcome new users!