Welcome, guest | Sign In | My Account | Store | Cart

Encodes the standard input channel using the rot13 scheme. Output goes to standard output.

Tcl, 12 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
#!/bin/sh
#     exec tclsh "$0" ${1:+"$@"}
proc main {} {
    set fromchars ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
    set tochars   NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm
    set map [list]
    foreach from [split $fromchars {}] to [split $tochars {}] {
        lappend map $from $to
    }
    puts stdout [string map $map [read stdin]]
}
main

3 comments

Jeff Hobbs 22 years, 7 months ago  # | flag

Static versus dynamic map generation. This builds up the rot13 map dynamically, which is nice and readable, but not as fast as a static declaration. Since rot13 isn't going to change, you might replace the dynamic map generation with a static map declaration like

set map [list A N B O C P D Q ...]
Glenn Jackman (author) 22 years, 7 months ago  # | flag

Incorrect invocation of tclsh. The first lines of the script should be:

#!/bin/sh
#     exec tclsh "$0" ${1:+"$@"}
Glenn Jackman (author) 22 years, 7 months ago  # | flag

Using TclX. Using the Extended Tcl extension, one could write

package require Tclx
puts stdout [translit {A-Za-z} {N-ZA-Mn-za-m} [read stdin]]
Created by Glenn Jackman on Mon, 10 Sep 2001 (MIT)
Tcl recipes (162)
Glenn Jackman's recipes (1)

Required Modules

  • (none specified)

Other Information and Tasks