title: get global tcl array in python or c++
body: <p>I need to import existing information from tcl files (saved variables in tcl) but I am not familiar with tcl really at all. I need a simple solutions (short), it does not need to be super robust.
  I have a tcl file that defines a global.  Here is a small snippet:</p>
  
  <p>test.tcl:</p>
  
  <pre><code>global gP
  set gP(1,1) {-2.77577 -2.66083 14.6978}
  set gP(1,2) {-2.2751 -1.91189 13.5285}
  set gP(1,3) {-1.46855 -1.21695 11.8953}
  ...
  set gP(1,spline) [list \
    {p (-0.005623,0.225326,4.821360) t (-0.155507,0.806054,-0.571047) tx (0.000000,0.578079,0.815981)} \{p (-0.006889,0.231865,4.816766) t (-0.156976,0.809595,-0.565609) tx (0.000000,0.572709,0.819758)} \
  ...
    {p (2.224570,6.136620,-6.119510) t (0.901583,0.422393,0.093441) tx (0.000000,0.215995,-0.976394)} \
    ]
  ...
  set gP(1,4) {-0.673205 -0.728409 10.3576}
  set gP(1,5) {-0.261822 -0.476418 9.47673}
  set gP(1,name) {path1}
  ...
  </code></pre>
  
  <p>What I want to do is run tcl from either python or c++ to produce gP in that language.  So something like (in python:)</p>
  
  <pre><code>from Tkinter import Tcl
  MYFILE = 'test.tcl'
  tcl = Tcl()
  gP = tcl.evalfile(MYFILE)
  </code></pre>
  
  <p>My end goal is to get this into c++, but I don't need all of gP. One problem I ran into doing this so far, is that gP is an array, and therefore will not return in the normal way in Tkinter.</p>
  
  <p>This doesn't need to be elegant, I'll only use this once or twice. I am willing to manipulate the variable in python, print out just what I need in a text (or CSV) file, then read that into c++ to populate a variable. </p>
  
  <p><strong>EDIT</strong></p>
  
  <p>Doing this as patthoyts described is a really great start!  I think I need one (or two levels of parsing?</p>
  
  <p>I have done this (gPathPoints and gP are the same, different varname only):</p>
  
  <pre><code>a  = eval(tcl.eval('''set r {};
    foreach k [array names gPathPoints] {
      lappend r "'$k': '$gPathPoints($k)'"
    };
    set r "{[join $r ,]}"
    '''))
  </code></pre>
  
  <p>Python spits out a decent dictionary, but the keys are a little funky:</p>
  
  <pre><code>'100,0': '-2.939 -3.02 15.136',
  '100,1': '-2.77577 -2.66083 14.6978',
  '100,10': '-0.00681104 0.408274 3.53018',
  ...
  </code></pre>
  
  <p>I can probably figure this out with a simple c++ method but I think the following would be easier in tcl:</p>
  
  <pre><code>'100,spline': '{p (-2.939000,-3.020000,15.136000) t (0.242924,0.657821,-0.712923) tx (0.000000,0.734938,0.678134)} {p (-2.934674,-3.008616,15.123467) t (0.250593,0.647200,-0.719955) tx (0.000000,0.743684,0.668531)}
  </code></pre>