Popular recipes by andreas kupries http://code.activestate.com/recipes/users/117230/2002-08-22T16:34:35-07:00ActiveState Code RecipesBits to Hex, and back (Tcl)
2002-08-21T16:48:53-07:00andreas kuprieshttp://code.activestate.com/recipes/users/117230/http://code.activestate.com/recipes/146037-bits-to-hex-and-back/
<p style="color: grey">
Tcl
recipe 146037
by <a href="/recipes/users/117230/">andreas kupries</a>
(<a href="/recipes/tags/text/">text</a>).
</p>
<p>Code to convert a string of '0's and '1's to the corresponding hex number, and back. Note that we are _not_ talking about binary strings. Code by Jeff Hobbs, from his bag of utilities.</p>
Inserts thousand separators into a number (Tcl)
2002-08-22T15:58:31-07:00andreas kuprieshttp://code.activestate.com/recipes/users/117230/http://code.activestate.com/recipes/146220-inserts-thousand-separators-into-a-number/
<p style="color: grey">
Tcl
recipe 146220
by <a href="/recipes/users/117230/">andreas kupries</a>
(<a href="/recipes/tags/graphics/">graphics</a>, <a href="/recipes/tags/math/">math</a>).
</p>
<p>This piece of code inserts a thousandth separator into a decimal number, i.e. every 3 digits the separator character is inserted. courtesy Jeff Hobbs's bag of utilities.</p>
Check creditcard numbers for wellformed-ness. (Tcl)
2002-08-22T16:03:11-07:00andreas kuprieshttp://code.activestate.com/recipes/users/117230/http://code.activestate.com/recipes/146221-check-creditcard-numbers-for-wellformed-ness/
<p style="color: grey">
Tcl
recipe 146221
by <a href="/recipes/users/117230/">andreas kupries</a>
(<a href="/recipes/tags/text/">text</a>).
</p>
<p>The command below checks if the specified creditcard number is syntactically valid. This code does __not__ check if there is actually creditcard with this number. Only that the number in itself is well-formed according to the rules of the creditcard companies. See the comments preceding the code for a list of the rules.</p>
Largest int supported by a platform, and the number of bits per int (Tcl)
2002-08-21T16:24:52-07:00andreas kuprieshttp://code.activestate.com/recipes/users/117230/http://code.activestate.com/recipes/146035-largest-int-supported-by-a-platform-and-the-number/
<p style="color: grey">
Tcl
recipe 146035
by <a href="/recipes/users/117230/">andreas kupries</a>
(<a href="/recipes/tags/graphics/">graphics</a>, <a href="/recipes/tags/math/">math</a>).
</p>
<p>Two procedures from the bag of utilities used by Jeff Hobbs. The first computes the largest integer supported by Tcl on the platform it is run on. The second computes the number of bits in that integer.</p>
Find smallest square to fit input. (Tcl)
2002-08-21T16:29:28-07:00andreas kuprieshttp://code.activestate.com/recipes/users/117230/http://code.activestate.com/recipes/146036-find-smallest-square-to-fit-input/
<p style="color: grey">
Tcl
recipe 146036
by <a href="/recipes/users/117230/">andreas kupries</a>
(<a href="/recipes/tags/graphics/">graphics</a>, <a href="/recipes/tags/math/">math</a>).
</p>
<p>A procedure from the bag of utilities used by Jeff Hobbs. Given an integer number it finds the smallest square greater than the input. A possible application for this is when a discrete number of items have to be displayed in a table. Using this returns the number of columns/rows for the table to fit all items.</p>
Lexing C (Tcl)
2002-08-22T16:34:35-07:00andreas kuprieshttp://code.activestate.com/recipes/users/117230/http://code.activestate.com/recipes/146223-lexing-c/
<p style="color: grey">
Tcl
recipe 146223
by <a href="/recipes/users/117230/">andreas kupries</a>
(<a href="/recipes/tags/debugging/">debugging</a>).
</p>
<p>Lexing C sources into a list of tokens. Demonstration of the string processing facilities of Tcl. The same approach will work for essentially all keyword based languages, for example Pascal, module, Eiffel, etc.
<p>
List and quoting based languages (Tcl itself, Lisp, ...) can be done too, but this is not optimal.
<p>
Author: Andreas Kupries <br>
Original location: <a href="http://mini.net/tcl/3906<br>" rel="nofollow">http://mini.net/tcl/3906<br></a></p>
Adding numbers in arbitrary bases. (Tcl)
2002-08-22T16:16:22-07:00andreas kuprieshttp://code.activestate.com/recipes/users/117230/http://code.activestate.com/recipes/146222-adding-numbers-in-arbitrary-bases/
<p style="color: grey">
Tcl
recipe 146222
by <a href="/recipes/users/117230/">andreas kupries</a>
(<a href="/recipes/tags/graphics/">graphics</a>, <a href="/recipes/tags/math/">math</a>).
</p>
<p>The command adds two numbers in any base that can be expressed as a string of unique symbols. From Jeff Hobbs's bag of utilities, original author is John Ellson <ellson@lucent.com>.</p>
Dump a file in hex and ASCII (Tcl)
2002-06-17T15:27:20-07:00andreas kuprieshttp://code.activestate.com/recipes/users/117230/http://code.activestate.com/recipes/133525-dump-a-file-in-hex-and-ascii/
<p style="color: grey">
Tcl
recipe 133525
by <a href="/recipes/users/117230/">andreas kupries</a>
(<a href="/recipes/tags/text/">text</a>).
</p>
<p>Origin: <a href="http://wiki.tcl.tk/1599" rel="nofollow">http://wiki.tcl.tk/1599</a>
Author: Original author unknown</p>
<p>Demonstrates the use of the [binary] and [fconfigure] commands to process a binary file.</p>
<p>The following useful Tcl script produces a hex/ASCII dump of a binary file whose name is specified on the command line.</p>
Minimal SOAP (Tcl)
2002-06-27T16:55:02-07:00andreas kuprieshttp://code.activestate.com/recipes/users/117230/http://code.activestate.com/recipes/136604-minimal-soap/
<p style="color: grey">
Tcl
recipe 136604
by <a href="/recipes/users/117230/">andreas kupries</a>
(<a href="/recipes/tags/distributed/">distributed</a>).
</p>
<p>A minimal example setting up and using SOAP to call to a temperature conversion service. Uses http and TclSOAP. The latter can be found at <a href="http://tclsoap.sourceforge.net" rel="nofollow">http://tclsoap.sourceforge.net</a></p>
Simple Tkhtml web page displayer (Tcl)
2002-06-27T17:06:48-07:00andreas kuprieshttp://code.activestate.com/recipes/users/117230/http://code.activestate.com/recipes/136605-simple-tkhtml-web-page-displayer/
<p style="color: grey">
Tcl
recipe 136605
by <a href="/recipes/users/117230/">andreas kupries</a>
(<a href="/recipes/tags/binding/">binding</a>).
</p>
<p>Author: Neil Madden
Origin: <a href="http://wiki.tcl.tk/2993" rel="nofollow">http://wiki.tcl.tk/2993</a></p>
<p>The application fetches a web page, formats it, and displays
it in a tk scrollable widget. It currently does not handle
redirecting URLs, making the web pages active, or ease of
changing fonts, etc. Requires TkHTML.</p>
Convert HSV colorspace to RGB (Tcl)
2002-06-17T16:08:36-07:00andreas kuprieshttp://code.activestate.com/recipes/users/117230/http://code.activestate.com/recipes/133527-convert-hsv-colorspace-to-rgb/
<p style="color: grey">
Tcl
recipe 133527
by <a href="/recipes/users/117230/">andreas kupries</a>
(<a href="/recipes/tags/graphics/">graphics</a>, <a href="/recipes/tags/math/">math</a>).
</p>
<p>Author: Jeff Hobbs.</p>
<p>Converts a color specified as Hue, Saturation and Value into an RGB triple. The result can be processed by "dec2rgb" (See recipe "Color Manipulation").</p>
Color Manipulation (Tcl)
2002-06-17T16:12:16-07:00andreas kuprieshttp://code.activestate.com/recipes/users/117230/http://code.activestate.com/recipes/133529-color-manipulation/
<p style="color: grey">
Tcl
recipe 133529
by <a href="/recipes/users/117230/">andreas kupries</a>
(<a href="/recipes/tags/graphics/">graphics</a>, <a href="/recipes/tags/math/">math</a>).
</p>
<p>Author: Jeff Hobbs</p>
<p>A number of routines to convert and manipluate colors.</p>
Functional imaging (Tcl)
2002-06-17T15:00:37-07:00andreas kuprieshttp://code.activestate.com/recipes/users/117230/http://code.activestate.com/recipes/133502-functional-imaging/
<p style="color: grey">
Tcl
recipe 133502
by <a href="/recipes/users/117230/">andreas kupries</a>
(<a href="/recipes/tags/graphics/">graphics</a>, <a href="/recipes/tags/math/">math</a>).
</p>
<p>Origin: <a href="http://wiki.tcl.tk/3523" rel="nofollow">http://wiki.tcl.tk/3523</a>
Author: Richard Suchenwirth</p>
<p>Cameron Laird pointed me to Conal Elliott's Pan project ("Functional Image Synthesis", [http://research.microsoft.com/~conal/papers/bridges2001/]), where images (of arbitrary size and resolution) are produced and manipulated in an elegant functional way. Functions written in Haskell (see Playing Haskell) are applied, mostly in functional composition, to pixels to return their color value. FAQ: "Can we have that in Tcl too?"
As the funimj demo below shows, in principle yes; but it takes some patience (or a very fast CPU) - for a 200x200 image the function is called 40000 times, which takes 9..48 seconds on my P200 box. Still, the output often is worth waiting for... and the time used to write this code was negligible, as the Haskell original could with few modifications be represented in Tcl. Functional composition had to be rewritten to Tcl's Polish notation - Haskell's</p>
<p>foo 1 o bar 2 o grill</p>
<p>(where "o" is the composition operator) would in Tcl look like
o {foo 1} {bar 2} grill</p>
<p>As the example shows, additional arguments can be specified; only the last argument is passed through the generated "function nest":
proc f {x} {foo 1 [bar 2 [grill $x]]}</p>
<p>But the name of the generated function is much nicer than "f": namely, the complete call to "o" is used, so the example proc has the name
"o {foo 1} {bar 2} grill"</p>
<p>which is pretty self-documenting ;-)</p>
Dragon Curve (Tcl)
2002-06-11T15:39:26-07:00andreas kuprieshttp://code.activestate.com/recipes/users/117230/http://code.activestate.com/recipes/132605-dragon-curve/
<p style="color: grey">
Tcl
recipe 132605
by <a href="/recipes/users/117230/">andreas kupries</a>
(<a href="/recipes/tags/graphics/">graphics</a>, <a href="/recipes/tags/math/">math</a>).
</p>
<p>Origin: <a href="http://wiki.tcl.tk/3349" rel="nofollow">http://wiki.tcl.tk/3349</a>
Author: Keith Vetter</p>
<p>Here's a quick and dirty rendering of the Dragon Curve or Paper Folding fractal. What happens if you repeatedly fold a piece of paper in half numerous times then unfold it once 90 degrees, then again 90 degrees and so on. You get a fractal curve which has several interesting properties. For example, you can combine 4 of these curves and not only will they not overlap but they fill the plane. For more details see <a href="http://www.math.okstate.edu/mathdept/dynamics/lecnotes/node17.html" rel="nofollow">http://www.math.okstate.edu/mathdept/dynamics/lecnotes/node17.html</a></p>
Convert HLS colorspace to RGB (Tcl)
2002-06-17T16:10:13-07:00andreas kuprieshttp://code.activestate.com/recipes/users/117230/http://code.activestate.com/recipes/133528-convert-hls-colorspace-to-rgb/
<p style="color: grey">
Tcl
recipe 133528
by <a href="/recipes/users/117230/">andreas kupries</a>
(<a href="/recipes/tags/graphics/">graphics</a>, <a href="/recipes/tags/math/">math</a>).
</p>
<p>Author: Frederic Bonnet</p>
<p>Converts a color in HLS to a RGB triple. The triple can be processed by "dec2rgb" (See recipe "Color Manipulation").</p>
Octabug, 3D to 2D transformation (Tcl)
2002-06-17T15:37:20-07:00andreas kuprieshttp://code.activestate.com/recipes/users/117230/http://code.activestate.com/recipes/133526-octabug-3d-to-2d-transformation/
<p style="color: grey">
Tcl
recipe 133526
by <a href="/recipes/users/117230/">andreas kupries</a>
(<a href="/recipes/tags/graphics/">graphics</a>, <a href="/recipes/tags/math/">math</a>).
</p>
<p>Origin: <a href="http://wiki.tcl.tk/3494" rel="nofollow">http://wiki.tcl.tk/3494</a>
Author: Keith Vetter</p>
<p>This is a fun little graphical animation which morphs an octahedron into an open cuboctahedron and back. Hidden within this code is actually a simple 3-d to 2-d transformation package.</p>
Converting numbers from arbitrary bases (Tcl)
2002-06-17T15:12:58-07:00andreas kuprieshttp://code.activestate.com/recipes/users/117230/http://code.activestate.com/recipes/133524-converting-numbers-from-arbitrary-bases/
<p style="color: grey">
Tcl
recipe 133524
by <a href="/recipes/users/117230/">andreas kupries</a>
(<a href="/recipes/tags/graphics/">graphics</a>, <a href="/recipes/tags/math/">math</a>).
</p>
<p>Origin: <a href="http://wiki.tcl.tk/1067" rel="nofollow">http://wiki.tcl.tk/1067</a>
Author: Michael A. Cleverly</p>
<p>The other day someone on <a href="http://OpenACS.org" rel="nofollow">OpenACS.org</a> asked [http://openacs.org/bboard/q-and-a-fetch-msg.tcl?msg_id=0000k2&topic_id=11&topic=OpenACS] for a Tcl proc that would convert a base-62 number into a base-10 integer. I replied with a version I'd written. Here is a slightly expanded one. convert_number employs some Salt and Sugar which I quite like.</p>
<p>(One caveat is that base_n_to_decimal will either return an incorrect answer or generate an error for really large numbers that are > than 2147483647.)</p>
Fuzzy Google truth (Tcl)
2002-06-11T15:40:48-07:00andreas kuprieshttp://code.activestate.com/recipes/users/117230/http://code.activestate.com/recipes/132606-fuzzy-google-truth/
<p style="color: grey">
Tcl
recipe 132606
by <a href="/recipes/users/117230/">andreas kupries</a>
(<a href="/recipes/tags/text/">text</a>).
</p>
<p>Origin: <a href="http://wiki.tcl.tk/3490" rel="nofollow">http://wiki.tcl.tk/3490</a>
Author: Richard Suchenwirth</p>
<p>Given some piece of data where it is doubtful whether they are correct or not, one way to find out is just to ask a search engine like Google, but disregard the results except for the number of found web pages. Chances are that the correct data have a higher hit rate than the faulty one.</p>
<p>Example output in the text widget (asking about a city in Italy, where post code and province were unsure):
'bellaria rn': 3580 hits
'bellaria fo': 609 hits
'bellaria 47814': 1130 hits
'bellaria 47014': 30 hits</p>
<p>These results seem to indicate that 47814 Bellaria RN (Rimini) is the correct address ;-) On single words one might use this for spelling verification:
'suchenwirth': 280 hits
'suchenworth': 0 hits</p>
<p>..or to check how strong an association between several words is:
'suchenwirth tcl': 57 hits
'suchenwirth java': 14 hits</p>
<p>The numbers may change over time, but the tendency ("fuzzy truth") can at least be estimated.</p>
Getting stock quotes over the internet (Tcl)
2002-06-11T15:33:05-07:00andreas kuprieshttp://code.activestate.com/recipes/users/117230/http://code.activestate.com/recipes/132604-getting-stock-quotes-over-the-internet/
<p style="color: grey">
Tcl
recipe 132604
by <a href="/recipes/users/117230/">andreas kupries</a>
(<a href="/recipes/tags/text/">text</a>).
</p>
<p>Origin: <a href="http://wiki.tcl.tk/636" rel="nofollow">http://wiki.tcl.tk/636</a>
Author: The original author is unknown.</p>
<p>Here's a simple Tcl script which will fire up a window to track a small portfolio of stocks. Nothing too fancy. I've included an example portfolio of three stocks, and a cash holding. To see your own portfolio, simply edit the 'shares' array, and the 'cash' variable</p>
Querying an online dictionary (Tcl)
2002-06-11T15:29:11-07:00andreas kuprieshttp://code.activestate.com/recipes/users/117230/http://code.activestate.com/recipes/132603-querying-an-online-dictionary/
<p style="color: grey">
Tcl
recipe 132603
by <a href="/recipes/users/117230/">andreas kupries</a>
(<a href="/recipes/tags/text/">text</a>).
</p>
<p>Origin: <a href="http://wiki.tcl.tk/3211" rel="nofollow">http://wiki.tcl.tk/3211</a>
Author: Reinhard Max.</p>
<p>This little script sends it's command line arguments as a query to the online dictionary at <a href="http://dict.leo.org" rel="nofollow">http://dict.leo.org</a> and writes the parsed result to stdout. It uses Tcl's http package and the htmlparse and ncgi packages from Tcllib.</p>
<p>The scraper part (everything inside the ::dict.leo.org namespace) could also be included from other frontends. It's [query] proc takes a list of words to search for, and returns a list of english/german pairs that matched the query.</p>