|  | Posted by comp.lang.tcl on 04/21/06 17:01 
slebetman@yahoo.com wrote:> 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}]]
 
 
 Thanx for the proc, however, tclsh locks up tight, bringing down PHP
 and Apache and all web services in the process, if you try this:
 
 set string [flatten [PROPER_CASE {{-hello} {world}}]]
 
 If you have a string with curly braces and a dash, it blows up.  Take
 one or the other away, all is well.
 
 Phil
  Navigation: [Reply to this message] |