Re: problem with [array get ...] -> file -> string -> [array set ...]



On Aug 3, 2:25 pm, Greg.A.Fisc...@xxxxxxxxx wrote:
Folks,

I'm pulling my hair out trying to save and load data to a file. I've
got a large array (probably filled with all sorts of "special"
characters), here's what I'm trying:

# To save:

puts $out "RxnInfo: '[array get RxnInfo]'"
...

#
# this appears in the file as "RxnInfo: '{Np-237 nf Cs-137,corr} 1.0
{Ni-58 np Co-58,corr} 1.0 ... '
#

# To load:

while {[gets $in line] != -1} {
regexp {^(.*): '(.*)'$} $line result varname value
if {[string match "*RxnInfo*" $varname ]} {
global $varname
array set $varname [list " $value "]
}
...

I've tried every conceivable permutation of quotation marks, the list
command, and curly braces, but I can't get the $value string to load
into RxnInfo (I've tried replacing "$varname" with RxnInfo explicitly
- same thing happens). It comes back with the "list must have an even
number of elements" error. The most infuriating thing is that if I
insert "puts [list " $value "]" right above the "array set" statement,
I can take the output from the "puts" command, copy it into tclsh, and
it works no problem.

Does anyone have any idea what is going on here, and, more
importantly, can they suggest a fix? Thanks a ton.

Greg
You can also use source cmd like below and the Tclx extension has an
lgets command :
---------------- START SCRIPT
#!/bin/sh
# the next line restarts using wish \
exec /opt/usr/bin/tclsh8.5 "$0" ${1+"$@"}
array set test { 1 a 2 b 3 c {abc 4} d }
parray test
puts "saving test to test.save"
# SAVING TO FILE
set fd [ open "/tmp/test.save" "w" ]
puts $fd "array set test { [ array get test] }";
close $fd
# REMOVING Test
array unset test
catch { parray test } err
puts "$err"
puts "retrieving test from test.save"
source /tmp/test.save
parray test


------------ END SCRIPT---------------
OUTPUT:
test(1) = a
test(2) = b
test(3) = c
test(abc 4) = d
saving test to test.save
"test" isn't an array
retrieving test from test.save
test(1) = a
test(2) = b
test(3) = c
test(abc 4) = d

Carl

.



Relevant Pages

  • Re: Array Troubles
    ... puts 'Please type your title for your table (if you type nothing the ... through the array. ... # split turns our string into an array, it uses a comma for a delimiter ...
    (comp.lang.ruby)
  • Re: fast stable sort
    ... For fastest load, at expense of slower sort, you ask the operating ... and you memory-map the file directly into that array. ... memory of your big array. ... possible to assure locality of reference and thus avoid thrashing ...
    (comp.programming)
  • Re: Controller for Switching current sink
    ... The load, includes ... the switch closed with my monster-FET array. ... So, I'm gonna precharge the cap, then throw the switch at 0v. ... I calculate 1.5 joules in the cap, so the fet has to dump the same 1.5 ...
    (sci.electronics.design)
  • Re: Cant convert Float into String
    ... That assigns 'Prune' to name (so name is no longer pointing to an Array, ... puts name == 'Prune' ... Given that name is a String at this point, ...
    (comp.lang.ruby)
  • Re: How can I create a 2D ArrayList? String usually.
    ... I basically would like a 2 column array or whatever that will be static, ... in values won't change after I load them and I would like to fairly quickly ... > Array.IndexOf is probably going to yield linear performance as you add ... you might be better off using hashtables. ...
    (microsoft.public.dotnet.languages.csharp)