http://www.perlmonks.org?node_id=1065677


in reply to Homework Golf

35 (OR 46 depending upon which of the scoring method used in this thread you favour):

#23456789_123456789_123456789_123456789_123456 # 123456789_123456789_123456789_12345 perl -nlE"unpack('%c*',$_&chr(31)x30)-65||say" words.txt

On *nix, it can be 1 shorter (correct nested 's; thanks choroba):

#23456789_123456789_123456789_123456789_12345 # 123456789_123456789_123456789_1234 perl -nlE'unpack("%c*",$_&"\x1f"x30)-65||say' words.txt

And one shorter still:

#23456789_123456789_123456789_123456789_12345 # 123456789_123456789_123456789_1234 perl -nlE'unpack("%c*",$_&"\c_"x30)-65||say' words.txt

With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

Replies are listed 'Best First'.
Re^2: Homework Golf (35)
by eyepopslikeamosquito (Archbishop) on Dec 05, 2013 at 09:38 UTC

    On *nix ... one shorter still:
    #23456789_123456789_123456789_123456789_1234 perl -nlE'unpack("%c*",$_&"\c_"x30)-65||say' words.txt
    Two more strokes can be shaved I believe (untested, no Unix box available right now):
    #23456789_123456789_123456789_123456789_12 perl -nlE'65^unpack"%c*",$_&~"`"x30or say' words.txt
    Update: an alternative using v31:
    #23456789_123456789_123456789_123456789_12 perl -nlE'65^unpack"%c*",$_&v31 x30or say' words.txt

      I (breifly) looked for a replacement for "\c_", but I was trying to xor two bareword chars together to produce chr(31). I found several pairs that worked, but then as ^ has lower precedence than x, I couldn't extend the string cos if you bracket the expression, x produces a list not a string :(. Didn't think of &~'`'. Or vstrings:( )

      Also couldn't see how to ditch the unpack parens.

      As neither of the strings needs escape processing (unlike "\c_"), you can switch the "s <-> 's and that works on windows also:

      C:\test>perl -nlE"65^unpack'%c*',$_&~'`'x30or say" words.txt | wc -l 1279

      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.
      There’s a third alternative using uc :
      #23456789_123456789_123456789_123456789_12 perl -nlE'65^unpack"%c*","?"x30&uc or say'
      Those spaces around or are annoying... Let's use something else to express conditionnal printing. With -p, x= does the job:
      #23456789_123456789_123456789_123456789_1 perl -ple'$_ x=65==unpack"%c*","?"x30&uc'
      While we're looking at those switches... Why the -l? Removing it and adding ord("\n") (10) to the target (65) shaves another stroke:
      #23456789_123456789_123456789_123456789_ perl -pe'$_ x=75=~unpack"%c*","?"x30&uc'
      There's a much shorter solution using a 6-bit checksum. The problem is that it comes up with a few false positives (words whose value is 1, 129, 193...):
      #23456789_123456789_123456789_1234 perl -ple'$_ x=1==unpack"%6c*",uc' perl -pe'$_ x=11==unpack"%6c*",uc'
      Using a 5-bit checksum removes the need for uc, but brings even more false positives:
      #23456789_123456789_123456789_1 perl -ple'$_ x=1==unpack"%5c*"' perl -pe'$_ x=11==unpack"%5c*"' perl -nlE'1^unpack"%5c*"or say' perl -nE'11^unpack"%5c*"or say'

        I'm pretty sure we have a winner here, at least in the perl5 division. Congrats for making me go look up x= ("Get outta here, that's legal? So it is!") and for the brilliant insight that lets you shave off the -l. +10 internets to Grimy!

Re^2: Homework Golf (35)
by McD (Chaplain) on Dec 05, 2013 at 02:09 UTC

    Wow, I love this.

    $_&"\c_"x30

    ...is about as close to executable line noise as I've seen in a long, long time. Bravo!

    If I'm smart, I'll borrow all these tricks, next time I come up with a golf idea. :-)