Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: Shorten this one-liner!

by eyepopslikeamosquito (Archbishop)
on Aug 26, 2018 at 12:22 UTC ( [id://1221130]=note: print w/replies, xml ) Need Help??


in reply to Shorten this one-liner!

Update: Current shortest version is
perl -E'/a/&&say"$_: ".y/e//for anyone,cancel,declare,perlmonks'
It's your competition, so you're free to set the rules ... though be aware that your solution, while short, is not super robust or portable. Re robustness, try changing "declare" to "delete" :) -- that is, while it works for your four chosen words, there are many barewords that will require quoting. Re portability, note that -E'...' works fine on Unix but fails on Windows (which needs -E"..." instead).

BTW, since you're interested in code golf, you might be interested in some of the articles I've written over the years on this topic, for example:

and many more listed in the Golf section of my home node eyepopslikeamosquito.

Replies are listed 'Best First'.
Re^2: Shorten this one-liner!
by lunix (Novice) on Aug 26, 2018 at 15:22 UTC
    Wow! Very interesting reading, you have provided, I like it much.
    This task (printing the word, if it contains "a" and then the occurrences of "e") came up years ago in python programming class and some could't do it. I was just trying to show them how uncomplicated it was by taking my perl code and turning it into a one-liner. Guess I was trying to be cool. And that was it, it was saved on my hard drive and forgotten. Now, years later I found the one-liner, which looked like (obviously with different words):

    perl -e '$c=0;@s=("anyone","cancel","declare","perlmonks");foreach(@s){if($s[$c]=~/a/){$n=()=$s[$c]=~/e/g;print"$s[$c]: $n\n";}$c++;}'
    (135 strokes)

    And I had to laugh. This didn't contain any really "perl-y" tricks (except for the regexp maybe), it was my original program without any whitespaces, and shorter variables. So in a few hours I got it down to

    perl -e 'for('anyone','cancel','declare','perlmonks'){$i=()=/e/g;print"$_: $i\n"if(/a/);}'
    (91 strokes)

    And here I got stuck. But I enjoyed getting it shorter, and I knew it could be even shorter. So I posted it here, to see how far I was from the shortest solution. Now we have

    perl -E'/a/&&say"$_: ".y/e//for anyone,cancel,declare,perlmonks'
    (65 strokes)

    And it might get shorter. I don't know.
    Also, this isn't really a competition, more like, just me trying to improve my skills. I was curious how differently others might approach it.

    PS. I never heard about code golf before, but thanks for introducing me to it, I might get into it with time :)

    Regards, lunix
      To win at normal golf, you have to have the minimum number of strokes for the course.
      Perl "Golf" is similar, the shortest code that does the job (fewest key stokes) wins!

      This Perl golf stuff is meaningless aside from an interesting puzzle.
      I recommend getting solid with your Perl coding skills before writing one-liners.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (2)
As of 2024-04-26 06:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found