Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Golf: Files Containing a Pattern

by kelan (Deacon)
on Oct 18, 2002 at 21:20 UTC ( [id://206439]=perlmeditation: print w/replies, xml ) Need Help??

Here's a fun little hole: Write a function that runs under use strict; which returns a list of the files in the current directory that contain a certain pattern. The pattern to match is passed to the function. Here's an example usage:
use strict; sub filegrep { ... } my @files = filegrep('Match this!');

My best is 56:

sub filegrep { # 1 2 3 4 5 #12345678901234567890123456789012345678901234567890123456 my@k;for(<*>){open F,$_;push@k,$_ if grep/$_[0]/,<F>};@k }
How many strokes can you shave?

kelan


Yak it up with Fullscreen ChatterBox

Replies are listed 'Best First'.
Re: Golf: Files Containing a Pattern
by Aristotle (Chancellor) on Oct 18, 2002 at 22:51 UTC
    # 1 2 3 #234567890123456789012345678901234 grep{open F,$_;grep/$_[0]/,<F>}<*>
    Update:
    # 1 2 3 #2345678901234567890123456789012 grep{@ARGV=$_;grep/$_[0]/,<>}<*>
    Has caveats of course..

    Makeshifts last the longest.

      Changing your 2nd grep to a map saves 1.
      #234567890123456789012345678901234 grep{open F,$_;map/$_[0]/,<F>}<*>
      --
      Pug
        Getting a little crazy can save three more strokes:
        grep{open _,$_;map/@_/,<_>}<*>
        Not much wiggle room left, I think.
Re: Golf: Files Containing a Pattern
by Juerd (Abbot) on Oct 19, 2002 at 11:13 UTC

    All solutions thus far are broken. They don't list dotfiles :)

    - Yes, I reinvent wheels.
    - Spam: Visit eurotraQ.
    

Re: Golf: Files Containing a Pattern
by abell (Chaplain) on Oct 18, 2002 at 22:38 UTC
    (44) 42.
    #!/usr/bin/perl use strict; sub filegrep { # 1 2 3 4 5 #12345678901234567890123456789012345678901234567890123456 my$a=$_[0];grep{open F,$_;grep/$a/,<F>}(<*>) }

    Antonio

    Update: gets down to 42 omitting the parenthesis in (<*>). It becomes
    #12345678901234567890123456789012345678901234567890123456 my$a=$_[0];grep{open F,$_;grep/$a/,<F>}<*>

    Update2: As pointed out by others, the usage of variable $a is totally useless.
    Update3: If $a were of any use, one wouldn't need my anyway, since $a and $b are special (think sort) and don't upset strict.
    Update4: As observed by crazyinsomniac, $a becomes global refers to the package global $a without my.which is a bad thing. This is a potentially bad thing.

    The stupider the astronaut, the easier it is to win the trip to Vega - A. Tucket
Re: Golf: Files Containing a Pattern
by BUU (Prior) on Oct 19, 2002 at 03:07 UTC
    sub filegrep { # 1 2 3 #1234567890123456789012345678901234 map{@ARGV=($_);grep{/$_[0]/}<>}<*> }
    which gives me a total of 34, as i couldnt think of a better way to get the value into ARGV =[
Re: Golf: Files Containing a Pattern
by Pug (Monk) on Oct 18, 2002 at 22:51 UTC
    I borrowed a bit/most of your code but how about 39
    # 1 2 3 4 #1234567890123456789012345678901234567890 grep{open F,$_;$_ if grep/$_[0]/,<F>}<*>
    Update: Added Code Tags. --
    Pug
Re: Golf: Files Containing a Pattern
by kelan (Deacon) on Oct 19, 2002 at 14:25 UTC
    Just wondering, but anyone know why this was moved from Obfu to Meditations? I would have thought golf was more of and obfu kind of thing.

    kelan

      But you're wrong. :) Obfu intentionally tries to mislead the reader, which is not the case for golfing, and to this end, complex obfus are often the very opposite of a golf - using verbosity to conceil the real intent.

      Go to Super Search, put "golf" in the "match titles" field and select "do not include replies" at the bottom of the page. You'll notice that nearly all golf challenges are in Meditations.

      Makeshifts last the longest.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlmeditation [id://206439]
Approved by erzuuli
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (2)
As of 2024-04-24 16:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found