Rosetta Stone for Scripting Languages

Inspired by the incredibly useful rosetta stone for UNIX sysadmins. Also, see the awesome Hyperpolyglot site.

Use click, shift-click, control-click, and the "Set All" and "Clear All" buttons to select the desired subset of scripting languages. Then select "Draw table" to redraw the window with your custom table.
Language sh ksh csh awk perl tcl python ruby JavaScript
Website www.kornshell.com www.perl.org www.tcl.tk www.python.org www.ruby-lang.org
Author Steve Bourne David Korn Bill Joy Aho, Weinberger & Kernighan Larry Wall et al John K. Ousterhout Guido van Rossum Yukihiro Matsumoto Brendan Eich
License AT&T Open Source License GPL / Perl Artistic License BSD-style CNRI Open Source GPL-Compatible license GPL
Single Line Invocation sh -c "script" ksh -c "script" csh -c 'script' awk 'script' perl -e 'script' echo 'script' | tclsh python -c script ruby -e 'script'
Script File Invocation sh scriptfilename ksh scriptfilename csh scriptfilename awk -f scriptfilename perl scriptfilename tclsh scriptfilename python scriptfilename ruby scriptfilename
Whitespace Mostly not significant Mostly not significant Not significant Not significant Not significant Mostly not significant, but commands and arguments need to be whitespace delimited Significant: indentation indicates blocks Newlines significant Newlines significant
Statement Separator ; or newline ; or newline ; or newline ; or newline ; ; or newline Newline ; or newline ; or newline
Block { } { } { } and [ ] and even " " (Indicated by increased indentation) { } or
do
  code
end
{ }
Comment # # # # # # or ;# for end-of-line comments # # or
=begin
comments
=end
// or /* */
Variable var when setting; ${var} or $var otherwise var when setting; ${var} or $var otherwise var when setting; ${var} or $var otherwise var $var var when setting; ${var} or $var otherwise var var (local variable), @var (instance variable),
@@var (class variable), $var (global variable), VAR (constant)
var x
Array ${array[n]} ($wordlist[n] for a space separated string) (Hashes used) $array[n] or
@array for whole thing;
$array[0] for first element
set list {a b c d}; access with lindex list n (called "lists") (1,2,3) tuple (immutable),
[1,2,3] list (mutable)
var[n] array[n]
Array Size ${#array[*]} $#wordlist The number of keys in the hash can be retrieved with:
n=0;for(key in hash){n=n+1}
and the largest key in the hash can be retrieved with:
m=0;for(key in hash){if(m<key)m=key}
$#array + 1 llength list len sequence array.length array.length
Hash hash[key] $hash{key} or
%hash for whole thing
hash(key) or array set hash {key value key value ... ...} (called "arrays") { key:value,..., key:value} var[key] object.key or object["key"]
Hash Iterate for (key in hash) block foreach $key (keys %hash) foreach key [array names hash] for key,value in hash.items() for (key in object)
String "quoted string" (with variable/command substitution),
'quoted string' (without variable/command substitution)
"quoted string" (with variable/command substitution),
'quoted string' (without variable/command substitution)
"quoted string" (with variable/command substitution),
'quoted string' (without variable/command substitution)
"quoted string" "quoted string" (with variable/command substitution),
'quoted string' (without variable/command substitution)
"quoted string" (with variable/command substitution, can include line breaks)
{quoted string} (without variable/command substitution, can include line breaks)
'Quoted string'
"Quoted string"
'''Quoted string including
line breaks'''
'quoted string' or %q{quoted string} or
"quoted string" or %Q{quoted string};
the latter two do substitution on #{code}
"quoted string" or 'quoted string'
Command line arguments $0,$1,$n where n is $# (and only up to $9; after that, use shift) $0,$1,$n where n is $# (need form ${n} for n>9) ARGV @ARGV argv list and argc variable sys.argv ARGV
Last Result $_ $_ _ (but only for explicit expressions in an interactive session) $_ (deprecated)
Assignment var=value var=value set var = value var = value $var = value; set var value var = value = =
Equality (string) = = == == eq == or eq == == == or ===
Equality (numeric) -eq -eq == == == == == == == or ===
Logical And -a -a or && && && && && and && or and &&
Logical Or -o -o or || || || || || or || or or ||
Logical Not ! ! ! ! ! ! not ! or not !
Regexp Search /regexp/
or match(string, regexp)
/regexp/ regexp pattern string import re
reobj = re.compile(regexp)
reobj.search(string)
(for repeated searches)
or (for one-time matching)
re.search(regexp, string)
string =~ /regexp/ string.match(/regexp/)
Regexp Replace sub(regexp, replacestring, targetstring) s/regexp/replacement/ regsub pattern string replacestring resultvar result = reobj.sub(replacement, string)
or
result = re.sub(regexp, replacement, string)
string.sub(/regexp/, replacementstring) or
string.gsub(/regexp/, replacementstring)
Conditional if condition
then block
elif condition
then block
else block
fi
if condition
then block
elif condition
then block
else block
fi
if (expr) then
  commands
else if (expr) then
  commands
else
  commands
endif
if (condition) block
else block
if (test) {block}
elsif (test) {block}
else {block}
if {condition} block
elseif {condition} block
else block
if condition:
  block
elif condition:
  block
else:
  block
if expression [then|:]
  body
elsif expression [then|:]
  body
else
  body
end
if (test) {block}
else if (test) {block}
else {block}
Iteration for item in list
do
  block
done
for item in list
do
  block
done
foreach item (wordlist)
  commands
end
for (var=lower; var <=upper; var++) block
for (item in array) block
for ($i=1; $i<10; $i++) {block}
for (10,9,8,7,6,5,4,3,2,1) {block}
for (1..15) {block}
foreach $element (@array) {block}
for {set i 0} {$i <= 5} {incr i} block
foreach item $list block
for x in iterable:
  block
arrayvar.each do |var|
  body
end

or
for var in arrayvar
  body
end
for (i=1; $i<10; $i++) {block}
for (variable in object) {block}
Loop while condition
do
  block
done
while condition
do
  block
done
while (condition)
  commands
end
while (condition) block while (test) {block} while {condition} block while condition:
  block
while expression [do|:]
  body
end
while (test) {block}
Next Loop Iteration continue continue continue continue next continue continue next continue
Exit Loop break break break break last break break break break
Switch case value in
  pattern1) commands;;
  pattern2) commands;;
esac
case value in
  pattern1) commands;;
  pattern2) commands;;
esac
switch (string)
case pattern:
  commands
  breaksw
case pattern:
  commands
  breaksw
default:
  commands
  breaksw
endsw
switch $var {
  a block
  b block
  default block
}
case
when condition [then|:]
  body
when condition [then|:]
  body
else
  body
end

or
case target
when comparison [then|:]
  body
when comparison [then|:]
  body
else
  body
end
switch (x) {
case number:
  commands
  break;
case strin:
  commands
  break;
default:
  commands
  break;
}
Function Definition fnname () { commands; } fnname () { commands; } or
function fnname { commands; }
function fnname (paramlist)
  block
} {
sub fnname {block} with arguments in @_ proc fnname {arg1 arg2 ..} block def fnname(param1, param2,..):
  block
def fnname(param1,param2)
  body
end
function fnname(arg1,arg2) {block}
Function Call fnname fnname fn(param1, param2) &fnname(arg1, arg2) fn arg1 arg2 fn(arg1, arg2, ..) fnname(arg1, arg2) fnname(arg1, arg2)
Function Parameters pass by Value Value Value Value or reference (with @@@) Value or reference (using upvar) Value Value Value for primitive types, reference for objects
Print to stdout print print echo print, printf print STDOUT "stuff to print\n",
printf STDOUT format ...
puts string print puts document.write
Open File for Read open(FILEHANDLE, "filename") set f [open filename r] open(filename, mode='r') file = File.open("filename", "r")
Open File for Write open(FILEHANDLE, ">filename") set f [open filename w] open(filename, mode='w') file = File.open("filename", "w")
Read from File read (only from stdin) read (-u n specifies file descriptor) $< reads line from stdin getline var < file <FILEHANDLE> gets $f var f.read() line = file.gets
Write to File print -u n (n specifies file descriptor) print FILEHANDLE "stuff to print\n" puts $f string f.write(string) file.puts
Close File close(filename) close(FILEHANDLE) close $f f.close() file.close
Test File test -r readable,
test -f is plain file,
test -d is directory;
[ condition ] is alternative form
test -r readable,
test -f is plain file,
test -d is directory;
[ condition ] is alternative form
-r readable,
-e exists,
-f is plain file,
-d is directory
-r readable,
-e exists,
-f is plain file,
-d is directory
file readable filename
file exists filename
file isfile filename
file isdirectory filename
os.path.exists(filename)
os.path.isfile(filename)
os.path.isdir(filename)
File.readable?("filename")
File.exist?("filename")
File.file?("filename")
File.directory?("dirname")
Delete File rm filename rm filename rm filename unlink("filename") file delete filename os.remove(filename) File.delete("filename")
Rename File mv oldfilename newfilename mv oldfilename newfilename mv oldfilename newfilename rename("oldname", "newname") file rename oldname newname os.rename(oldname, newname) File.rename("oldname", "newname");
File Info ($dev, $ino, $mode, $nlink, $uid,
$gid, $rdev, $size, $atime, $mtime, $ctime,
$blksize, $blocks) = stat(FILEHANDLE)
file stat filename resultarray os.stat(filename) File.stat("filename")
Output Formatting C-like printf formats C-like printf formats C-like format C-like printf
String Concatenation "stringa" "stringb" "stringa" . "stringb" "$stringa$stringb" or append command string1+string2 "stringa" + "stringb" "stringa" + "stringb"
Substring ${stringvar:offset:length} (bash only) substr(string, offset [,length]) substr(string, offset [,length]) string range string offset last string[offset:endoffset] string[start, len] string.substring(startpos, endpos)
String Tokenization split(string, array, [,separator]) split /separator/ expression split string separatorCharacters string.split(separator) string.split(/separator/) string.split(delimiter)
Language sh ksh csh awk perl tcl python ruby JavaScript

Please let me know if you spot anything wrong or missing in the table.

Helpful references used in the compilation of this table include:

Thanks to:

Other links of interest:

This table is produced from a collection of XML source files using an XSLT stylesheet.


Back to Home Page


Contact me