We all know that laziness, and not necessity, is the mother of invention. Can you craft shorter code than that listed below? Not only are you defending your code against other coders, but defending your scripting language of choice against the others. Coders, get coding.

Rules:

Other Notes:

Contributors:

Related links here at parseerror.com:

Task: Perl: Python: PHP: Ruby:
Print 1-10 on seperate lines in html 22 chars: print"$_<br>"for 1..10 36 chars: for i in range(1,11): print'%d<br>'%i 29 chars: echo join('<br>',range(1,10)) 26 chars: puts((1..10).map*('<br>'))
Print A-Z on own lines 15 chars: $,=$/;print A..Z 33 chars: for i in range(65,91): print'%c'%i 35 chars: while($n<26) echo chr(++$n+64)."\n"; 24 chars: 'A'.upto('Z'){|x|puts x}
Print a string in slices of 3 characters on newline (assigned prior) 19 chars: #assumes string as $_ $,=$/;print/.{1,3}/g 43 chars: #assumes string as s for i in range(0,len(s),3): print s[i:i+3] 41 chars: //assumes string as $s while($o=substr($s,(3*$c++),3)) echo "$o\n"; 25 chars: #assumes string as $_ scan(/.{1,3}/){|n|puts n}
Create hash mapping A-Z to
n-1
14 chars: @h{A..Z}=0..25 41 chars: h={} for i in range(26): h[chr(i+65)]=i 43 chars: while($n<26) $h[chr(++$n+64)]=count($h); 34 chars: h={} ('A'..'Z').map{|x| h[x]=h.size }
Print out every line from 'f.txt' containing a number 28 chars: perl -ne'/\d/&&print' f.txt 61 chars: import re for l in open('f.txt'): if re.search('\d',s):print l 57 chars: foreach(file('f.txt') as $l) echo(ereg('[0-9]',$l)?$l:''); 31 chars: ruby -ne'/\d/and puts $_' f.txt
Print the number of times the substring 'fish' appears in file 'f.txt' 37 chars: perl -ne'$n+=()=/fish/g}{die$n' f.txt 59 chars: import re print len(re.findall('fish', open('f.txt').read())) 47 chars: <?=substr_count(join('', file('f.txt'),'fish')?> 43 chars: p File.new('f.txt').read. scan('fish').size
Assign the last line in file 'f.txt' to variable 'v' 21 chars: perl -ne'$v=$_' f.txt 31 chars: v=open('f.txt').readlines()[-1] 29 chars: $v=array_pop(file('f.txt')); 20 chars: ruby -ne'v=$_' f.txt
Print out every directory in root 21 chars: -d&&print$_.$/for</*> 52 chars: import os def p(a,d,n):print d os.path.walk('/',p,0) 79 chars: $d=opendir('/'); while($f=readdir($d)) if(is_dir("/$f")) echo "$f\n"; closedir($d); 45 chars: Dir.foreach('/'){|d| puts d if test(?d,'/'+d) }
Loop that prompts with '>>' echoes input, exits on 'exit' 44 chars: $\='>>';print; print/^exit$/?exit:$_ while<> 90 chars: $x=fopen('/dev/stdin','r'); echo '>>'; while($t=fgets($x,1024)) echo $t=="exit\n"?die:"$t>>"; 48 chars: print'>>' while gets print/^exit$/?exit:$_,'>>' end
Connect to MySQL and return all fields joined with '|' from all records returned. 129 chars: use DBI;$l=DBI->connect( "DBI:mysql:$db",$u,$p); $s=$l->do("SELECT * FROM TBL"); while(@r=$s->fetchrow_array) print join('|',@r)."\n" 116 chars: mysql_connect($h,$u,$p); $r=mysql_query("SELECT * FROM $db.TBL"); while($x=mysql_fetch_row($r)) echo join('|',$x)."\n"; 101 chars: require "mysql" l=Mysql.new('h','u','p','db') r=l.query("SELECT * FROM TBL") r.each{|f| puts f.join('|') }
Create a simple class in which to store Song data. 106 chars: package Song; my($title,$artist,$length); sub new{ ($self,$title,$artist,$length)=@_; bless \$self } 76 chars: class Song: def __init__(s,t,a,l): s.title=t s.artist=a s.lenth=l 87 chars: class Song{ function Song($t,$a,$l){ $this->title=$t; $this->artist=$a; $this->length=$l; } } 69 chars: class Song def initialize(t,a,l) @title=t @artist=a @length=l end end
Grab www.yahoo.com and list every link on the page. 73 chars: use LWP::Simple;$,=$/; print/ef=([^> ]+)>/g for get"http://www.yahoo.com"' 210 chars: import httplib, re h=httplib.HTTP("www.yahoo.com") h.putrequest("GET","/index.html") h.putheader("Accept','text/html") h.endheaders() h.getreply() ll=re.findall('ef=.*?>', h.getfile().read()) for l in ll: print l[4:-2] 102 chars: preg_match_all('/ef=(.*?)>/', join('',file(' http://www.yahoo.com')),$m); while($x=next($m[1])) echo "$x\n"; 92 chars: re 'net/http' n=Net::HTTP.new('www.yahoo.com') r,d=n.get('/') puts d.scan(/ef=(.*?)>/)