| View previous topic :: View next topic |
| Author |
Message |
CrazyCat Revered One

Joined: 13 Jan 2002 Posts: 1032 Location: France
|
Posted: Tue Nov 16, 2010 8:04 am Post subject: Two questions: namespaces and arrays |
|
|
Hello there,
I've two small questions, I hope you can help me:
First, not really important
I've a code like this
| Code: | namespace eval wowa {
variable wchans "test"
variable chars
proc init { args } {
set [namespace current]::chars $::wowa::wchans
}
} |
Is there a way to replace $::wowa::wchans by an expression as ${[namespace current]}::wchans ?
Second question, about arrays (in a namespace)
I'm trying to get a structure of datas like:
chars(me)(0) { "item1" "bla" "item2" "bee" }
chars(me)(1) { "item1" "bla" "item2" "bee" }
chars(him)(0) { "item1" "bla" "item2" "bee" }
chars(him)(1) { "item1" "bla" "item2" "bee" }
So, I've created this small proc:
| Code: | foreach line [split $wdata "\n"] {
set char [split $line "|"]
if { [array exists [namespace current]::chars([lindex $char 0])]} {
putlog "Info exists"
set val [array size [namespace current]::chars([lindex $char 0])]
putlog "Setted"
} else {
set val 0
}
putlog "Val is $val"
set [namespace current]::chars([lindex $char 0])($val)(nick) [lindex $char 1]
putlog "Setted: $::wowa::chars([lindex $char 0])($val)(item1)"
set [namespace current]::chars([lindex $char 0])($val)(item2) [lindex $char 2]
putlog "Gives: [array size [namespace current]::chars([lindex $char 0])]"
} |
$wdata contains lines:
me|test|nothing
me|test1|nothing too
him|bla|test
him|bli|test
This is completely wrong, anyone has an idea ?
Thanks a lot. _________________ https://www.eggdrop.fr - French IRC network
Offer me a coffee - Do not ask me help in PM, we are a community. |
|
| Back to top |
|
 |
nml375 Revered One
Joined: 04 Aug 2006 Posts: 2857
|
Posted: Tue Nov 16, 2010 3:16 pm Post subject: |
|
|
First part:
I'd just use the variable command in a similar manner to the global command:
| Code: | namespace eval wowa {
variable wchans "test"
variable chars
proc init { args } {
variable wchans
variable chars
set chars $wchans
}
} |
As for the second part, I'm having a hard time following what you intend to achieve, and what the actual/expected results are. However, I do notice that you try to use multi-dimensional arrays. This is not supported by tcl, which will flatten the array index to whatever is between the outer (). Thus, array size ::wowa::chars([lindex $char 0]) will not produce the result you expect, but most likely always 0. _________________ NML_375, idling at #eggdrop@IrcNET |
|
| Back to top |
|
 |
CrazyCat Revered One

Joined: 13 Jan 2002 Posts: 1032 Location: France
|
Posted: Tue Nov 16, 2010 5:52 pm Post subject: |
|
|
Thanks a lot nml375.
Concerning my "array trouble", have you any alternative idea to solve my trouble ? Using dict seems peharps the solution ? _________________ https://www.eggdrop.fr - French IRC network
Offer me a coffee - Do not ask me help in PM, we are a community. |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|