2020-04-25

Tcl: loop (calculates the Harmonic series)

用來練習迴圈的問題。

#!/usr/bin/env tclsh
#
# Calculates the Harmonic series.
# h(n) = 1 + 1/2 + 1/3 + … + 1/n
#

if {$argc >= 1} {
    set N [lindex $argv 0]
} elseif {$argc == 0} {
    puts "Please input a number."
    exit    
}

if {[string is integer $N]==0} {
    puts "It is not a number."
    exit
}

set h 0.0
for {set count $N} {$count >= 1} {incr count -1} {
    set h [expr $h + 1.0 / $count]
}

puts [format "%5E" $h]

再來是使用 while 迴圈計算 Harmonic series 的程式:
#!/usr/bin/env tclsh
#
# Calculates the Harmonic series.
# h(n) = 1 + 1/2 + 1/3 + … + 1/n
#

if {$argc >= 1} {
    set N [lindex $argv 0]
} elseif {$argc == 0} {
    puts "Please input a number."
    exit    
}

if {[string is integer $N]==0} {
    puts "It is not a number."
    exit
}

set h 0.0
set I 1
while {$I <= $N} {
    set h [expr $h + 1.0 / $I]
    incr I
}

puts [format "%5E" $h]

2020-04-20

Tcl: Nth Item In A List

假設有一個 List,
set mylist [list 2 4 6 8 10]

如果要取得 List 中 nth 的 item,可以使用 lindex,如下面的例子:
lindex $mylist 1
這樣就會得到 4 這個值。

2020-04-12

Tcl: divmod

Given positive integers C and N, find N numbers that sum up to C and the difference between the highest and the lowest of these number should not be more than one. For example: with C = 26 and N = 7, the desired output is [4 4 4 4 4 3 3].

#!/usr/bin/tclsh

proc divmod {C N} {
    set answer [list]

    if {($C <= 0) || ($N <= 0) || ($C < $N)} {
        return $answer
    }

    set element [expr $C / $N]
    set remainder [expr $C % $N]
    
    for {set count 0} {$count < $N} {incr count} {
        lappend answer $element
    }

    if {$remainder != 0} {
        for {set count 0} {$count < $remainder} {incr count} {
            set myvalue [lindex $answer $count]
            incr myvalue 1
            lset answer $count $myvalue
        }
    }

    return $answer
}

puts -nonewline "Please input a number C: "
flush stdout
gets stdin C
puts -nonewline "Please input a number N: "
flush stdout
gets stdin N

set myanswer [divmod $C $N]
puts "The answer list: $myanswer"

2020-03-21

Tcl: print number

Write a program that displays the digits from 1 to n then back down to 1; for instance, if n = 5, the program should display 123454321. You are permitted to use only a single for loop. The range is 0 < n < 10.

if {$argc >= 1} {
    set n [lindex $argv 0]
} elseif {$argc == 0} {
    puts "Please input a number."
    exit
}

if {[string is integer $n]==0} {
    puts "It is not a number."
    exit
}

switch $n {
    {1} {puts "1"}
    {2} {puts "121"}
    {3} {puts "12321"}
    {4} {puts "1234321"}
    {5} {puts "123454321"}
    {6} {puts "12345654321"}
    {7} {puts "1234567654321"}
    {8} {puts "123456787654321"}
    {9} {puts "12345678987654321"}
    default {puts "Please input 0 < n < 10"}
}

使用 while 實作的話:
if {$argc >= 1} {
    set n [lindex $argv 0]
} elseif {$argc == 0} {
    puts "Please input a number."
    exit
}

if {[string is integer $n]==0} {
    puts "It is not a number."
    exit
}

if {$n < 1 || $n > 9} {
    puts "Please input 0 < n < 10"
    exit    
}

set positive 1
set count 0
while {1} {
   if {$positive == 1} {
       incr count
       puts -nonewline $count
       if {$count == $n} {
            set positive 0
            continue
       }
   } else {
       incr count -1
       if {$count > 0} {
            puts -nonewline $count
       } else {
            break 
       }
   }
}
puts ""

2020-03-19

Tcl: sha256

使用者在命令列輸入一個字串,然後程式計算字串 sha256 的值並且輸出:
#!/usr/bin/env tclsh
if {$argc >= 1} {
    set countString [lindex $argv 0]
} elseif {$argc == 0} {
    puts "Please input a string"
    exit
}

package require sha256
puts "String: $countString"
puts "Result: [string toupper [sha2::sha256 -hex $countString]]"

2020-03-09

Tcl: MD5

使用者在命令列輸入一個字串,然後程式計算字串 MD5 的值並且輸出:
#!/usr/bin/env tclsh
if {$argc >= 1} {
    set countString [lindex $argv 0]
} elseif {$argc == 0} {
    puts "Please input a string"
    exit
}

package require md5
puts "String: $countString"
puts "Result: [md5::md5 -hex $countString]"

2020-03-07

Tcl: file size

列出目前目錄的檔案與其檔案大小:
#!/usr/bin/env tclsh

foreach filename [glob -nocomplain -type f *] {
    puts "$filename: [file size $filename] bytes"
}

2020-03-03

tcl-lmdb v0.4.1

檔案放置網頁

tcl-lmdb - Tcl interface to the Lightning Memory-Mapped Database

About

This is the Lightning Memory-Mapped Database (LMDB) extension for Tcl using the Tcl Extension Architecture (TEA).

LMDB is a Btree-based database management library with an API similar to BerkeleyDB. The library is thread-aware and supports concurrent read/write access from multiple processes and threads. The DB structure is multi-versioned, and data pages use a copy-on-write strategy, which also provides resistance to corruption and eliminates the need for any recovery procedures. The database is exposed in a memory map, requiring no page cache layer of its own. This extension provides an easy to use interface for accessing LMDB database files from Tcl.

Main Change

  1. Update LMDB source code.
  2. Makefile.in: Remove workaround for glibc.


這是一個 checkpoint 版本,只是建立 tag 追蹤從上一個版本以來的變化。tcl-lmdb 本身是沒有變動的,只有在 Makefile.in 移除關於  glibc 的 workaround,以及更新 LMDB 的 source code。

2020-02-17

tklib 0.7

tklib 釋出了一個新的正式版 v0.7。

Overview
========

    5  new packages                in 5  modules
    11 changed packages            in 9  modules
    2  internally changed packages in 1  modules
    47 unchanged packages          in 19 modules
    79 packages, total             in 31 modules, total

New in tklib 0.7
================

    Module                Package               New Version   Comments
    --------------------- --------------------- ------------- ----------
    canvas                canvas::gradient      0.2
    notifywindow          notifywindow          1.0
    persistentSelection   persistentSelection   1.0b1
    scrollutil            scrollutil::common    1.5
    widgetPlus            widgetPlus            1.0b2
    --------------------- --------------------- ------------- ----------

Changes from tklib 0.6 to 0.7
=============================

                                         tklib 0.6     tklib 0.7
    Module          Package              Old Version   New Version   Comments
    --------------- -------------------- ------------- ------------- ----------------
    controlwidget   rdial                0.3           0.7           D EF EX
    crosshair       crosshair            1.1           1.2           B EF EX
    datefield       datefield            0.2           0.3           D EF
    mentry          mentry::common       3.6           3.10          B D EF I
    plotchart       Plotchart            2.1.0         2.4.1         B D EF I
    --------------- -------------------- ------------- ------------- ----------------
    tablelist       tablelist::common    5.7                         API B D EF I P
                    tablelist::common                  6.8           API B D EF I P
    --------------- -------------------- ------------- ------------- ----------------
    tooltip         tooltip              1.4.4         1.4.6         B D EF
    --------------- -------------------- ------------- ------------- ----------------
    wcb             Wcb                  3.4           3.6           B D EF I P
                    wcb                  3.4           3.6           B D EF I P
    --------------- -------------------- ------------- ------------- ----------------
    widgetl         widget::listentry    0.1.1         0.1.2         D I
                    widget::listsimple   0.1.1         0.1.2         D I
    --------------- -------------------- ------------- ------------- ----------------

Invisible changes (documentation, testsuites)
=============================================

                                    tklib 0.6     tklib 0.7
    Module          Package         Old Version   New Version   Comments
    --------------- --------------- ------------- ------------- ----------
    controlwidget   controlwidget   0.1           0.1           D
                    meter           1.0           1.0           EX
    --------------- --------------- ------------- ------------- ----------

critcl 3.1.18

critcl

ChangeLog:
  1. Feature (Developer support). Merged pull request #96 from sebres/main-direct-invoke. Enables direct invokation of the "main.tcl" file for starkits from within a dev checkout, i.e. outside of a starkit, or starpack.
  2. Feature. Added channel types to the set of builtin argument and result types. The argument types are for simple channel access, access requiring unshared channels, and taking the channel fully into the C level, away from Tcl. The result type comes in variants for newly created channels, known channels, and to return taken channels back to Tcl. The first will register the returned value in the interpreter, the second assumes that it already is.
  3. Bugfix. Issue #96. Reworked the documentation around the argument type Tcl_Interp* to make its special status more visible, explain uses, and call it out from result types where its use will be necessary or at least useful.
  4. Feature. Package critcl::class bumped to version 1.1. Extended with the ability to create a C API for classes, and the ability to disable the generation of the Tcl API.
  5. Bugfix. Merged pull request #99 from pooryorick/master. Fixes to the target directory calculations done by the install code.
  6. Merged pull request #94 from andreas-kupries/documentation. A larger documentation cleanup. The main work was done by pooryorick, followed by tweaks done by myself.
  7. Extended the test suite with lots of cases based on the examples for the various generator packages. IOW the new test cases replicate/encapsulate the examples and demonstrate that the packages used by the examples generate working code.
  8. Bugfix. Issue #95. Changed the field critcl_bytes.s to unsigned char* to match Tcl's type. Further constified the field to make clear that read-only usage is the common case for it.
  9. Bugfix/Feature. Package critcl::cutil bumped to version 0.2. Fixed missing inclusion of header "string.h" in "critcl_alloc.h", needed for memcpy in macro STREP. Added macros ALLOC_PLUS and STRDUP. Moved documentation of STREP... macros into proper place (alloc section, not assert).
  10. Merged pull request #83 from apnadkarni/vc-fixes. Removed deprecated -Gs for MSVC builds, and other Windows fixups.
  11. Feature. Package critcl::iassoc bumped to version 1.1. Refactored internals to generate an include header for use by .c files. This now matches what other generator packages do. The template file is inlined and removed.
  12. Merged pull request #82 from gahr/home-symlink Modified tests to handle possibility of $HOME a symlink.
  13. Merged pull request #81 from gahr/test-not-installed Modified test support to find uninstalled critcl packages when running tests. Handles all but critcl::md5.
  14. Merged pull request #85 from snoe925/issue-84 to fix Issue #84 breaking installation on OSX.
  15. Merged pull request #87 from apnadkarni/tea-fixes to fix Issue #86, broken -tea option, generating an incomplete package.
  16. Feature. New package critcl::callback providing C-level functions and data structures to manage callbacks from C to Tcl.
  17. Feature. Package critcl::literals bumped to version 1.3. Added mode +list enabling the conversion of multiple literals into a list of their strings.
  18. Feature. Package critcl::enum bumped to version 1.1. Added basic mode handling, supporting tcl (default) and +list (extension enabling the conversion of multiple enum values into a list of their strings).
  19. Feature. Package critcl::emap bumped to version 1.2. Extended existing mode handling with +list extension enabling the conversion of multiple emap values into a list of their strings.
  20. Feature. Extended the set of available types by applying a few range restrictions to the scalar types (int, long, wideint, double, float).
    Example: int > 0 is now a viable type name.
    This is actually more limited than the description might let you believe.
    See the package reference for the details.