NYCPHP Meetup

NYPHP.org

[nycphp-talk] Any PHP users here also know TCL?

james at surgam.net james at surgam.net
Sat Jun 7 19:45:50 EDT 2003



"soazine at pop.erols.com" says:
> Thanx for your solution.  Now I have to face another issue.  The "pollID
> $pollID" elements that were joined together into one element in the same
> list now have to be broken apart within the same list.  How do I do that
> too?

I'm not sure what you mean.  If you mean you want to create a list
where the same element will somehow act both as one element made up of
two pieces and also will sometimes act as two separate elements, you
can't do that.  If you want to put in three elements, you can do that.
For example, here's the Tcl to create a list with three such elements:

set pollID 15
set lstVar [list "pollID $pollID" pollID $pollID]

Now you have a list of three elements.  The first one is:

pollID 15

The second one is:

pollID

The third one is:

15

I think this is what you are talking about because, as you asked you
have the elements "joined together" and "broken apart within the same
list".

If you are saying you need a reliable way to split the elements
*after* you read them out of the list, you can do it like this:

set pollID 15
set lstVar [list thing1 thing2 "something borrowed" "something blue" "pollID $pollID"]
set numPollid [lsearch $lstVar "pollID $pollID"]
set lstPieces [split [lindex $lstVar $numPollid] { }]

In this example, first I created a list with the following five strings as
its elements:

thing1
thing2 
something borrowed
something blue
pollID 15

Then I found the index of the pollid entry, extracted that entry and
split it up on its internal space using the "split" command.  

You now have a list variable, called lstPieces, that contains the
split up pieces of the "pollID 15" element.  It contains two elements,
numbered 0 and 1.  Element # 0 of this small list is the string
"pollID" and element # 1 is the number 15.

We're sort of getting into the nitty-gritty of introductory Tcl here,
so if this is bugging anyone, just drop me a line and I'll refrain
from further off-topic emails via the list.

HTH,
James



More information about the talk mailing list