Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
 
Python and Tcl - public course schedule [here]
Private courses on your site - see [here]
Please ask about maintenance training for Perl, PHP, Lua, etc
 
Tcl - a new example for data reformatting

Tcl is an excellent language for combing through and reformatting data - though rarely a first choice language for such tasks if users aren't doing other things with it too.

Towards the end of last week's Learning to program in TCL course, a "side task" requirement came up to reformat our course diary into a .csv file so that a reseller who sells our courses to fill gaps / niches in his own more generic schedule.

Data of the form
  30,11,2015 EL 8
needs to be transformed into data of the form
  ELFULL,30/11/2015,09:00,04/12/2015,17:00,Standard,1,1,SN12 7NY,En-GB,1350,1350,GBP,Well House,Learning to Program in Perl,Other (IT)Code

Complete program [here] and sample data [here].

The main code is as simple as
  loadDataWH "diary" intoHere
  saveDataQA "qa.csv" intoHere

so you can see that even in Tcl, we're using good, structured programming techniques - in this case letting me provide a number of different input formats and a number of output genereators.

Other points of note ...

• calculating end date from start date, allowing for month and year ends:

  set starts [clock scan "$day,$month,$year 12 00" -format "%d,%m,%Y %H %M"]
  set ends [expr $starts + ($courselength -1) * 3600 * 24]
  set eday [clock format $ends -format %e]
  set emonth [clock format $ends -format %m]
  set eyear [clock format $ends -format %Y]


reduces day, month, year of start to seconds from 1.1.1970, adds the number of days of the course (*3600*24) to make it seconds, and converts back. Much easier than trying to work out month lengths, especially for February in leap years.

• course names ending in "P" are 4 days and "L" are five days:

  set courselength 5
  set courseformat {Learning to Program in %s}
  if {[regexp {P$} $coursecode]} {
    set courselength 4
    set courseformat {%s Programming}
  }


• use of upvar to pass big chunks of data out of and into procs effectively and easily.

• allowing blank lines on input to be ignored

  if {[string length $lyne] < 1} continue

• ... and many other useful techniques, worth a study!

(written 2015-10-10)

 
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
T209 - Tcl/Tk - File and Directory Handling
  [4523] Catching failed commands and not crashing the program in Tcl - (2015-10-10)
  [4461] Reading from a URL, and reading Json, from your Tcl script - (2015-03-12)
  [3617] The fileutil package and a list of file system commands in Tcl - (2012-02-18)
  [3429] Searching through all the files in or below a directory - Ruby, Tcl, Perl - (2011-09-09)
  [3320] Reading the nth line from a file (Perl and Tcl examples) - (2011-06-09)
  [3192] Tcl - Some example of HOW TO in handling data files and formats - (2011-03-04)
  [2467] Tcl - catching an error before your program crashes - (2009-10-22)
  [1467] stdout v stderr (Tcl, Perl, Shell) - (2007-12-10)
  [1426] Buffering up in Tcl - the empty coke can comparison - (2007-11-10)
  [1407] Reading from another process in Tcl (pipes and sockets) - (2007-10-26)
  [785] Running external processes in Tcl and Tcl/Tk - (2006-06-29)
  [779] The fragility of pancakes - and better structures - (2006-06-26)


Back to
Catching failed commands and not crashing the program in Tcl
Previous and next
or
Horse's mouth home
Forward to
What does Tcl do if you try to run a command that is not defined?
Some other Articles
Routing in Flask
Hello Flask world / Python web micro framework
Day trip to Lancaster
What does Tcl do if you try to run a command that is not defined?
Tcl - a new example for data reformatting
Loading packages in your Tcl program
Should we get an AA or Visit Wiltshire hotel assessment?
No cold sales calls please - but delighted to hear from others!
Course Schedule - October 2015 to June 2016
4759 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96 at 50 posts per page


This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2019: 404 The Spa • Melksham, Wiltshire • United Kingdom • SN12 6QL
PH: 01225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/4524_Tcl ... tting.html • PAGE BUILT: Sat May 27 16:49:10 2017 • BUILD SYSTEM: WomanWithCat