Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Golfing on a quiet Friday afternoon...

by hans_moleman (Sexton)
on Jul 26, 2002 at 20:04 UTC ( #185649=perlmeditation: print w/ replies, xml ) Need Help??

We're golfing in the cube farm today, I'm writing Perl code and my adversary is writing PHP. So far he's got me by one character on this one, but I can't see how I can make it any shorter.

The program must take a file name from the command line and return the number of characters in the file. Not the most ambitious requirements in the world for sure :). A newline at the end of the program output is required.

It would be nice if I could (get help) figuring out a way to restore Perl's bragging rights...


print((stat pop)[7],$/)

Comment on Golfing on a quiet Friday afternoon...
Download Code
Re: Golfing on a quiet Friday afternoon...
by japhy (Canon) on Jul 26, 2002 at 20:18 UTC
    Some evolution for you:
    #2345678901234567890123 print((stat pop)[7],$/) # 23 print+(stat pop)[7],$/ # 22 print+(-s pop),$/ # 17 print-s pop,$/ # 14
    Ta da. 14.

    _____________________________________________________
    Jeff[japhy]Pinyan: Perl, regex, and perl hacker, who'd like a job (NYC-area)
    s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;

      Thanks! very enlightening.

      I'm very new to golf and don't know the ground rules and was amazed by hans_moleman's solution and simply blown away by japhy's evolution, but.... (/me hear's the groans)...trying japhy's final answer on my win32 (that has -w permenantly enabled) system I got:

      C:\test>185653 185653.pl 16Use of uninitialized value in print at C:\test\185653.pl line 1. C:\test>

      Ignoring the warning which I could turn off, I noticed that it reports its own length (under win32 etc. etc) as 16!

      So, I played and came up with this, which (on my Wi...) does a little better and (visually at least :) complies with the 'rules'

      (I see a newline after the number :),

      and beats japhy's by 3!...and runs with -w and no errors!

      C:\test>185649 185649.pl 13 C:\test>

      And the code

      C:\test>type 185649.pl print-s pop C:\test>

      As an aside, in a unicode world, maybe the requirements should have been "number of bytes"?

        Ahh, certainly this is cheating BrowserUk {grin}.

        The version of emulated DOS on your Win32 system always places new command prompt on a separate line by itself. So, even if your perl script prints just '13' without a terminating '\n', DOS would do the job for you.

        However, for a Unix system I can't see how one could go shorter than just:
        #12345678901234 print-s pop,$/
        Which is 14 characters.

        Surely, had japhy considered writing his golf for DOS, then the ',$/' part would be dropped thereby reducing the golf even further by 3 characters as you've pointed out ;-)

        Update: To clarify, the word 'cheating' should be considered a sarcasm ;^). I didn't mean to sound offending. I very much respect BrowserUk's comment and have nothing against him pointing out the intimate features of perl running on various non *nix systems. I simply felt it would be worthwhile to also direct your attention to the minor difference in running the golf piece on Win32 and Unix systems.

        _____________________
        # Under Construction
      you missed one...

      #23456789012 die-s pop,$/

      ~Particle *accelerates*

Re: Golfing on a quiet Friday afternoon...
by Chmrr (Vicar) on Jul 26, 2002 at 21:59 UTC

    If you really do mean characters, not bytes, check out this recent thread.

    perl -pe '"I lo*`+$^X$\"$]!$/"=~m%(.*)%s;$_=$1;y^`+*^e v^#$&V"+@( NO CARRIER'

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (2)
As of 2014-07-12 23:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (242 votes), past polls