This is the new home of the egghelp.org community forum.
All data has been migrated (including user logins/passwords) to a new phpBB version.


For more information, see this announcement post. Click the X in the top right-corner of this box to dismiss this message.

Two questions: namespaces and arrays

Help for those learning Tcl or writing their own scripts.
Post Reply
User avatar
CrazyCat
Revered One
Posts: 1334
Joined: Sun Jan 13, 2002 8:00 pm
Location: France
Contact:

Two questions: namespaces and arrays

Post by CrazyCat »

Hello there,
I've two small questions, I hope you can help me:

First, not really important
I've a code like this

Code: Select all

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: Select all

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.
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

First part:
I'd just use the variable command in a similar manner to the global command:

Code: Select all

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
User avatar
CrazyCat
Revered One
Posts: 1334
Joined: Sun Jan 13, 2002 8:00 pm
Location: France
Contact:

Post by CrazyCat »

Thanks a lot nml375.

Concerning my "array trouble", have you any alternative idea to solve my trouble ? Using dict seems peharps the solution ?
Post Reply