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。