http://qs321.pair.com?node_id=46492


in reply to Detect common lines between two files, one liner from shell

While I can see how Stephen is looking at this as an obfuscation-in-progress, and others are looking at it as just plain obfuscated, this type of thing is a great example of why I love playing around with Perl. Don't get me wrong, I certainly thought merlyn had left out a few lines when I first looked at it, but after a couple of minutes it really started to look beautiful (I'm sick, I know...). In this fine example, merlyn:
- didn't redefine any defaults
- didn't use any obscure, poorly documented features
- didn't even use single-letter variable names
- heck, it's even full of wasted spaces

He simply made excellent use of the well-documented default behaviours which even I use every day. It still amazes me that St.Larry (and some Perl elves) thought up all these behaviours which often look odd to me at first, but eventually dovetail together so well it's hard to imagine all of these uses weren't considered. Maybe after I've been here long enough I'll be able to come up with better ways to fit the parts together too.

--
I'd like to be able to assign to an luser

  • Comment on Re: (thoughts on) Detect common lines between two files, one liner from shell

Replies are listed 'Best First'.
Re: Re: (thoughts on) Detect common lines between two files, one liner from shell
by merlyn (Sage) on Dec 14, 2000 at 05:26 UTC
    Yeah, it's not an obfuscation by any means. Just a nice way to put together a lot of common features. In fact, I'd take it one step further to fulfill one additional monkey wrench thrown in after that posting was made: what if the line appears more than once in either fileA or fileB or both, but you still want only one copy of the line?

    Well, the answer is just as straightforward. Remove the dollar from the regex! I'll leave that explanation as an exercise to the clever reader. {grin}

    -- Randal L. Schwartz, Perl hacker


    update: Bleh! My mistake, the dollar was added to handle this case! I knew I had needed to deal with multiple hits somehow.

    Remind me never to post again. {grin}

      This should also work for an arbitrary number of files, which AFAIK has no equivalent UNIX command. To show common lines in 4 files: perl -ne 'print if ($seen{$_} .= @ARGV) =~ /32+1+0$/' fileA fileB fileC fileD
        The generalized form would be (golfed down a bit) for any number of files
        perl -ne'BEGIN{$p.=-$_."+"for-@ARGV+1..-1;$p.=0}print if($s{$_}.=@ARGV +)=~/$p$/'

        --shmem

        _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                      /\_¯/(q    /
        ----------------------------  \__(m.====·.(_("always off the crowd"))."·
        ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}