|
Posted by slebetman@yahoo.com on 10/19/64 11:45
comp.lang.tcl wrote:
> I have a TCL proc that needs to convert what might be a list into a
> string to read
>
> consider this:
>
> [set string [PROPER_CASE {hello world}]]; # OUTPUTS Hello World which
> is fine for PHP
>
> [set string [PROPER_CASE {-hello world}]]; # OUTPUT {{-Hello}}
> World, which PHP will print literally as {{-Hello}} World, instead of
> -Hello World
>
> Is there an easy way for TCL to brute-force a list into a
> strictly-string-only format to prevent this potentially ugly display
> from being displayed by PHP/etc.?
The other replies here are right you may want to use join to "flatten"
your list. But I also think your [PROPER_CASE] proc is written wrongly
in that you intend it to return a string but instead it returns a list,
worse still a double nested list:
{
{
{
-Hello
}
}
World
}
which requires you to do [join] twice to flatten it out. I'd strongly
recommend that you look at the PROPER_CASE proc and correct the bug
there if it is under your control.
Of course, [join] only flattens a list by 1 level and since your list
is double nested you need to call [join] twice. But if you're not sure
how many levels deep your list is nested here's a small proc to
recursively flatten an arbitrarily nested list:
proc flatten {ls} {
set ret ""
foreach x $ls {
if {$x == "\{" || $x == "\}"} {
# handle braces:
lappend ret $x
} elseif {[llength $x] > 1} {
# recursively flatten sublist:
set ret [concat $ret [flatten $x]]
} else {
lappend ret $x
}
}
return $ret
}
So you can use it like:
set string [flatten [PROPER_CASE {-hello world}]]
Navigation:
[Reply to this message]
|