Skip to content

Instantly share code, notes, and snippets.

@arodland
Created April 29, 2017 04:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arodland/c061dd7b0207a9b77a768391c3ff3e64 to your computer and use it in GitHub Desktop.
Save arodland/c061dd7b0207a9b77a768391c3ff3e64 to your computer and use it in GitHub Desktop.
# starting length: 96
@a=("Hare ","Krishna ","Rama ","\n");foreach(split//,'ababdbbaadacacdccaad'){print $a[ord($_)-97]}
# unneeded whitespace (-1)
@a=("Hare ","Krishna ","Rama ","\n");foreach(split//,'ababdbbaadacacdccaad'){print$a[ord($_)-97]}
# magic variable substitution (-2)
@a=("Hare ","Krishna ","Rama ",$/);foreach(split//,'ababdbbaadacacdccaad'){print$a[ord($_)-97]}
# for is the same as foreach (-4)
@a=("Hare ","Krishna ","Rama ",$/);for(split//,'ababdbbaadacacdccaad'){print$a[ord($_)-97]}
# /./g is shorter than split (-2)
@a=("Hare ","Krishna ","Rama ",$/);for('ababdbbaadacacdccaad'=~/./g){print$a[ord($_)-97]}
# we don't need this ord business... make the characters of the string be indices! (-8)
@a=("Hare ","Krishna ","Rama ",$/);for('01013110030202322003'=~/./g){print$a[$_]}
# nor do we need a variable... (-5)
for('01013110030202322003'=~/./g){print+("Hare ","Krishna ","Rama ",$/)[$_]}
# postfix 'for' doesn't need parens or braces (-4)
print+("Hare ","Krishna ","Rama ",$/)[$_]for'01013110030202322003'=~/./g
# new length: 72 (-26)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment