Sujet : Re: Proper way to split a string?
De : rich (at) *nospam* example.invalid (Rich)
Groupes : comp.lang.tclDate : 21. Dec 2024, 22:12:01
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <vk7av1$7hb6$1@dont-email.me>
References : 1 2 3
User-Agent : tin/2.6.1-20211226 ("Convalmore") (Linux/5.15.139 (x86_64))
Luc <
luc@sep.invalid> wrote:
On Fri, 20 Dec 2024 23:13:44 -0600, Gerald Lester wrote:
Rich suggested making it a list. You can do that or make ::PINPUT an array:
>
set ::PINPUT(state) waiting
bind $::SRw <Return> {
set ::PINPUT(pname) $pname
set ::PINPUT(pname) $pfile
set ::PINPUT(state) done
}
>
then:
>
vwait ::PINPUT(state)
catch {destroy $::SRw}
return [array get ::PINPUT]
>
Where you go to use it, do:
>
array set resultArray $returnValue
>
Or use the dict command to access the pieces.
>
**************************
It's good to know that I was even wronger than I thought.
I have been using a list as Rich suggested, but the use of an array
is very interesting, rather Tcl-ish.
And array has the downside of you cannot directly pass an array into a
proc nor directly return an array from a proc [1].
Either of a list or a dict can be passed in and returned directly with
no further 'effort' needed on your part within your code.
A list gives you the data, but without any attached 'names', so you
have to remember that "index 2 is data value Y" elsewhere.
A dict gives you 'named values' (just like an array) so if you prefer
that method, then you can use a dict to 'return' the values out of a
proc, and they will have names just like with an array.
And, you can convert a dict to an array via "array set array_name
$dict_name". Converting an array to a dict works this way "set
dict_name [dict create {*}[array get array_name]]" or by just
'accessing' the list returned from [array get] with the [dict] command,
which will shimmer it into a dict. The explicit [dict create] method
does make it clear in your code that you are purposefully performing
the transform.
[1] Instead you have to do things like "array get name_of_array" to
return it, or pass in the name of the array and use upvar to access it.