Perl: the Markov chain saw | |
PerlMonks |
comment on |
( [id://3333]=superdoc: print w/replies, xml ) | Need Help?? |
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 In reply to Re^2: Shorten this one-liner!
by lunix
|
|