Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: Perl Golf 101

by whio (Sexton)
on May 27, 2006 at 01:32 UTC ( #551957=note: print w/replies, xml ) Need Help??


in reply to Perl Golf 101

A lot of times, golf is about shaving off a few characters here and there. I'm sure these have been pointed out before, but:
  • y///c is one character shorter than length
  • ($x=~/./g) is two characters shorter than (split//,$x)
  • bareword=> can save a character over 'bareword',
For control flow, you want for, map, and punctuation. map and for are largely interchangeable. while is almost never necessary. Don't forget about the C-language-style for(;;), although it's rarely a gain. Postfix your operators; statement-modifier for doesn't require the parens or the braces that make a BLOCK. Replace branching if() statements with ternary or logical operators. Precedence rules will help here; also, remember that in Perl, true is '1' and false is the empty string. If you don't need the short-circuiting, you can use the bitwise flavor of & and |. Know which builtin variables are used and what special effects they have. ($- and $=, I'm looking at you.)

From the snippets you have posted:

$o .= $p >= 0 ? $a[$p] : $_;
could become $o .= $p < 0 ? $_ : $a[$p] and
$r =~ s/[^a-z]//g;
could become $r =~ y/a-z//cd;

Replies are listed 'Best First'.
Re^2: Perl Golf 101
by chargrill (Parson) on May 27, 2006 at 06:45 UTC

    Excellent tips! I'll have to incorporate those into my golfed obfu, though I shan't post an updated version because its format is quite well fixed with the existing number of characters it had prior to the application of some of the tips received since posting. Since the first round of feedback from this node, I've already cut down another ~60 characters, and with yours I'm sure I'll cut down quite a few as well (Update: around another 20 so far), as the original makes a fair amount of use of some of the idioms you're suggesting which can be golfed further.

    I sort of hinted at postfixing for not requiring the parens of braces without stating it explicitly, along with order of precedence.

    As far as builtin variables, while I know what $- and $= are (and default to), I don't have anything in my notes about special effects they have - just a note for $* making any numerical value assigned to it an implicit int (which gives me an idea... ;-)

    I will more than likely incorporate some of your suggestions (and especially your explanations!) into my original post, with of course your permission (and proper attribution on my part).



    --chargrill
    $,=42;for(34,0,-3,9,-11,11,-17,7,-5){$*.=pack'c'=>$,+=$_}for(reverse s +plit//=>$* ){$%++?$ %%2?push@C,$_,$":push@c,$_,$":(push@C,$_,$")&&push@c,$"}$C[$# +C]=$/;($#C >$#c)?($ c=\@C)&&($ C=\@c):($ c=\@c)&&($C=\@C);$%=$|;for(@$c){print$_^ +$$C[$%++]}
      As you point out, $* converts numerical values to int. $= is similar except that it converts everything to int. $- is the same as $= except it can't go negative, or higher than INT_MAX. ($= seems to be able to go up to UINT_MAX.)

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (4)
As of 2019-12-09 04:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?