You are here: Re: How do you convert a TCL list into a string that PHP can read? « PHP Programming Language « IT news, forums, messages
Re: How do you convert a TCL list into a string that PHP can read?

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]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация