Variable size/Set: Difference between revisions

Content added Content deleted
(→‎{{header|Tcl}}: + length limit)
Line 46: Line 46:
In Tcl, all values are (Unicode) strings. Their size is measured in characters, and the minimum size of a string is of course 0.
In Tcl, all values are (Unicode) strings. Their size is measured in characters, and the minimum size of a string is of course 0.
However, one can arrange, via write traces, that the value of a variable is reformatted to bigger size. Examples, from an interactive [[tclsh]] session:
However, one can arrange, via write traces, that the value of a variable is reformatted to bigger size. Examples, from an interactive [[tclsh]] session:
<lang Tcl>% proc format_trace {fmt _var el op} {upvar 1 $_var v; set v [format $fmt $v]}
<lang Tcl>
% proc format_trace {fmt _var el op} {upvar 1 $_var v; set v [format $fmt $v]}


% trace var foo w {format_trace %10s}
% trace var foo w {format_trace %10s}
Line 55: Line 54:
% trace var grill w {format_trace %-10s}
% trace var grill w {format_trace %-10s}
% puts "/[set grill bar]/"
% puts "/[set grill bar]/"
/bar /
/bar /</lang>
..or limit its size to a certain length:
%
<lang Tcl>% proc range_trace {n _var el op} {upvar 1 $_var v; set v [string range $v 0 [incr n -1]]}
</lang>

% trace var baz w {range_trace 2}
% set baz Frankfurt
Fr</lang>


{{Omit From|E}}
{{Omit From|E}}