Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

Golfing on a quiet Friday afternoon...

by hans_moleman (Beadle)
on Jul 26, 2002 at 20:04 UTC ( [id://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],$/)

Replies are listed 'Best First'.
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:??;

      you missed one...

      #23456789012 die-s pop,$/

      ~Particle *accelerates*

      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 16Use of uninitialized value in print at C:\test\ 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 13 C:\test>

      And the code

      C:\test>type 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

      Thanks! very enlightening.

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'