Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: Print all derefs and method calls in a directory (Friday golf)

by ikegami (Patriarch)
on Jan 09, 2009 at 10:20 UTC ( [id://735144]=note: print w/replies, xml ) Need Help??


in reply to Print all derefs and method calls in a directory (Friday golf)

Addressing the golfing request,

# 123 rgrep -e'->' * | perl -ne '/(\S{3,80}->\S{5,80})/;print $1,"\n"' | pe +rl -pe's/^\(//' | perl -pe 's/^\+\+|^\-\-//' | sort | uniq

is sufficiently equivalent to

# 68 perl -ne's/^(\(|[+-]{2})//g,say for/(\S{3,80}->\S{5,80})/' *|sort -u

And since you already assume that you can't have two dereferencing expressions on the same line,

# 67 perl -ne's/^(\(|[+-]{2})//g,say for/\S{3,80}->\S{5,80}/g' *|sort -u

Replies are listed 'Best First'.
Re^2: Print all derefs and method calls in a directory (Friday golf)
by JavaFan (Canon) on Jan 09, 2009 at 12:11 UTC
    Since you're using 'say', you have to use '-E' instead of '-e'.

    Note also that [+-]{2} is not equivalent to \+\+|-- as the former matches "-+" while the latter doesn't. Also, the original code will remove a leading "(--" from the line, while your code will not (it will either remove a leading '(' or a leading '--', '-+', '+-' or '--'). Finally, a /g modifier is useless if the pattern is anchored to the beginning of the string. And, if you're golfing, you shouldn't make a capture if you aren't using the result.

    Your s/// could be written as:

    s/^\(?([-+])\1//
    My entry (untested):
    perl -nE'/\(?([-+])\1.*?(\S{3,80}->\S{5,80})/&&say$2' *|sort -u
    I do think the 80 is a bit arbitrary, and
    perl -nE'/\(?([-+])\1.*?(\S{3,}->\S{5,})/&&say$2' *|sort -u
    is not only shorter, but likely to be preferred.

      Since you're using 'say', you have to use '-E' instead of '-e'.

      Oops!

      it will either remove a leading '(' or a leading '--', '-+', '+-' or '--')

      Oops!

      [+-]{2} is not equivalent to \+\+|--

      Indeed, but I deemed it acceptable.

      if you're golfing, you shouldn't make a capture if you aren't using the result.

      Not until Perl6. /...(?:...|...)/ is longer than /...(...|...)/. (Multiplying out the leading subexpression would have been shorter, but that's not the issue you raised.)

      I take your 69 and give you 51:

      perl -nE'/[-+(]*(\S{3,}->\S{5,})/&&say$1' *|sort -u

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (6)
As of 2024-04-23 11:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found