File: foreachline.tcl

package info (click to toggle)
tcl9.0 9.0.2%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 35,924 kB
  • sloc: ansic: 217,401; tcl: 24,192; makefile: 3,536; sh: 2,570; ada: 1,681; pascal: 1,139; cpp: 1,001; cs: 879; yacc: 840; asm: 468; perl: 420; xml: 95
file content (25 lines) | stat: -rw-r--r-- 608 bytes parent folder | download | duplicates (3)
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
# foreachLine:
# Iterate over the contents of a file, a line at a time.
# The body script is run for each, with variable varName set to the line
# contents.
#
# Copyright © 2023 Donal K Fellows.
#
# See the file "license.terms" for information on usage and redistribution
# of this file, and for a DISCLAIMER OF ALL WARRANTIES.
#

proc foreachLine {varName filename body} {
    upvar 1 $varName line
    set f [open $filename "r"]
    try {
	while {[gets $f line] >= 0} {
	    uplevel 1 $body
	}
    } on return {msg opt} {
	dict incr opt -level
	return -options $opt $msg
    } finally {
	close $f
    }
}