<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en-gb">
	<link rel="self" type="application/atom+xml" href="https://forum.eggheads.org/app.php/feed/topic/17272" />

	<title>egghelp/eggheads community</title>
	<subtitle>Discussion of eggdrop bots, shell accounts and tcl scripts.</subtitle>
	<link href="https://forum.eggheads.org/index.php" />
	<updated>2009-10-30T15:47:33-04:00</updated>

	<author><name><![CDATA[egghelp/eggheads community]]></name></author>
	<id>https://forum.eggheads.org/app.php/feed/topic/17272</id>

		<entry>
		<author><name><![CDATA[nml375]]></name></author>
		<updated>2009-10-30T15:47:33-04:00</updated>

		<published>2009-10-30T15:47:33-04:00</published>
		<id>https://forum.eggheads.org/viewtopic.php?p=90702#p90702</id>
		<link href="https://forum.eggheads.org/viewtopic.php?p=90702#p90702"/>
		<title type="html"><![CDATA[Save array to file /  restore array from file]]></title>

		
		<content type="html" xml:base="https://forum.eggheads.org/viewtopic.php?p=90702#p90702"><![CDATA[
Using upvar, I can control at which level I should access the variable, and with a fixed name, while global only access the variable under the same name in globalspace. You could probably modify the code with ease to use global instead.<br><br>I'll give you a simple example, as I'm not sure you are keeping the array index name and it's value apart...<div class="codebox"><p>Code: </p><pre><code>set var "TestingTestingTesting"set myArray($var) "This is a valuespanning multiple linesand containing several words...The array index holding this value also has several newlines"##or simpler:set myArray(line\ 1\nline\ 2) "Some\ndata..."</code></pre></div>Both versions will add values to the myArray array, each value containing at least one newline, and each array index name also containing both newlines and spaces...<p>Statistics: Posted by <a href="https://forum.eggheads.org/memberlist.php?mode=viewprofile&amp;u=8052">nml375</a> — Fri Oct 30, 2009 3:47 pm</p><hr />
]]></content>
	</entry>
		<entry>
		<author><name><![CDATA[willyw]]></name></author>
		<updated>2009-10-30T15:34:43-04:00</updated>

		<published>2009-10-30T15:34:43-04:00</published>
		<id>https://forum.eggheads.org/viewtopic.php?p=90700#p90700</id>
		<link href="https://forum.eggheads.org/viewtopic.php?p=90700#p90700"/>
		<title type="html"><![CDATA[Save array to file /  restore array from file]]></title>

		
		<content type="html" xml:base="https://forum.eggheads.org/viewtopic.php?p=90700#p90700"><![CDATA[
I have been unable to spend serious time on my little project,   and maybe that is a blessing in disguise.   <img class="smilies" src="https://forum.eggheads.org/images/smilies/icon_smile.gif" width="15" height="15" alt=":)" title="Smile">      It might be best for me to wait, see where this very interesting conversation leads,   and then try to read and grasp it then.<br><br>I don't want to derail it, but I do have a couple of side note type questions:<br><br>regarding:   upvar 1 $varname v<br>is the use of upvar necessary?    I looked it up, can see that it allows you to pass into the proc the value from that variable that is not within that proc.<br>Why not simply:  <br>    global varname   <br>in this proc?     If it was global elsewhere, would that not let it work?<br>or... hmm..   maybe with upvar, you are allowing for the original var to NOT be global?... perhaps that is the point?     <br>This is a little above me,  I'll be interesting in learning, from whatever you reply with.    Tnx.<br><br>Other question<br>Regarding  " or if the indexname contains spaces. "<br>Is this possible?   I must not be understanding....   this is what it sounds like you are saying:<br>varname(some word,1 "string here")<br>varname(some word,2 "string here")  <br>and so on....<br><br>instead of <br>varname(someword,1 "string here")<br>varname(someword,2 "string here")<br>and so on....<br><br>Thanks<p>Statistics: Posted by <a href="https://forum.eggheads.org/memberlist.php?mode=viewprofile&amp;u=10420">willyw</a> — Fri Oct 30, 2009 3:34 pm</p><hr />
]]></content>
	</entry>
		<entry>
		<author><name><![CDATA[nml375]]></name></author>
		<updated>2009-10-30T15:03:27-04:00</updated>

		<published>2009-10-30T15:03:27-04:00</published>
		<id>https://forum.eggheads.org/viewtopic.php?p=90698#p90698</id>
		<link href="https://forum.eggheads.org/viewtopic.php?p=90698#p90698"/>
		<title type="html"><![CDATA[Save array to file /  restore array from file]]></title>

		
		<content type="html" xml:base="https://forum.eggheads.org/viewtopic.php?p=90698#p90698"><![CDATA[
Well, I'm not sure why you have this slow code reading line by line... Also, your code will drop any empty line, possibly corrupting/mangling data (or index names) containing empty lines within the data.<br><br>The problem with the idea of one record per line, is really that one record may span more than one line. That is, unless you are willing to generate proper escape sequences throughout the data, and the appropriate substitutions when restoring the array, along with all the hazzle and risk of introducing unexpected variable/command substitutions into the code...<p>Statistics: Posted by <a href="https://forum.eggheads.org/memberlist.php?mode=viewprofile&amp;u=8052">nml375</a> — Fri Oct 30, 2009 3:03 pm</p><hr />
]]></content>
	</entry>
		<entry>
		<author><name><![CDATA[arfer]]></name></author>
		<updated>2009-10-30T14:47:24-04:00</updated>

		<published>2009-10-30T14:47:24-04:00</published>
		<id>https://forum.eggheads.org/viewtopic.php?p=90697#p90697</id>
		<link href="https://forum.eggheads.org/viewtopic.php?p=90697#p90697"/>
		<title type="html"><![CDATA[Save array to file /  restore array from file]]></title>

		
		<content type="html" xml:base="https://forum.eggheads.org/viewtopic.php?p=90697#p90697"><![CDATA[
Thanks nml375. I have an additional question. Suppose I want my cake AND eat it. That is to say I want to guard against spaces or special characters or even non-printable characters in the array element names/values BUT I want the neatness/readability of one element (record if you like) per line in the file.<br><br>Would you please check out the following and advise :-<br><div class="codebox"><p>Code: </p><pre><code>proc saveArray {varname filename} {    upvar 1 $varname v    set fp [open $filename w]    foreach {name value} [array get v] {        puts $fp [list $name $value]    }    close $fp    return 0}proc restoreArray {varname filename} {    upvar 1 $varname v    set data ""    set fp [open $filename r]    while {![eof $fp]} {        set d [gets $fp]        if {[string length $d] != 0} {            append data $d        }    }    close $fp    array set v $data    return 0}</code></pre></div><p>Statistics: Posted by <a href="https://forum.eggheads.org/memberlist.php?mode=viewprofile&amp;u=5705">arfer</a> — Fri Oct 30, 2009 2:47 pm</p><hr />
]]></content>
	</entry>
		<entry>
		<author><name><![CDATA[nml375]]></name></author>
		<updated>2009-10-30T13:27:28-04:00</updated>

		<published>2009-10-30T13:27:28-04:00</published>
		<id>https://forum.eggheads.org/viewtopic.php?p=90694#p90694</id>
		<link href="https://forum.eggheads.org/viewtopic.php?p=90694#p90694"/>
		<title type="html"><![CDATA[Save array to file /  restore array from file]]></title>

		
		<content type="html" xml:base="https://forum.eggheads.org/viewtopic.php?p=90694#p90694"><![CDATA[
The best approach imho would be to simply keep the list returned from array get untouched, and to restore it in a similar fashion with array set. Whenever you are using split, you run into the risk of data being mangled unless you take precautions to make sure that the "split-char" is not part of the stored data entities.<br><br>A trivial, working, save/restore would look as follows:<div class="codebox"><p>Code: </p><pre><code>proc saveArray {varname file} { upvar 1 $varname v set fp [open $file w] puts -nonewline $fp [array get v] close $fp}proc restoreArray {varname file} { upvar 1 $varname v set data "" set fp [open $file r] while {![eof $fp]} {  append data [read $fp] } close $fp array set v $data}</code></pre></div><p>Statistics: Posted by <a href="https://forum.eggheads.org/memberlist.php?mode=viewprofile&amp;u=8052">nml375</a> — Fri Oct 30, 2009 1:27 pm</p><hr />
]]></content>
	</entry>
		<entry>
		<author><name><![CDATA[arfer]]></name></author>
		<updated>2009-10-30T02:50:03-04:00</updated>

		<published>2009-10-30T02:50:03-04:00</published>
		<id>https://forum.eggheads.org/viewtopic.php?p=90679#p90679</id>
		<link href="https://forum.eggheads.org/viewtopic.php?p=90679#p90679"/>
		<title type="html"><![CDATA[Save array to file /  restore array from file]]></title>

		
		<content type="html" xml:base="https://forum.eggheads.org/viewtopic.php?p=90679#p90679"><![CDATA[
Thanks for the input nml375, I might just review some of my old scripts on this basis.<br><br>I should have read the files given in the links rather than simply trying to interpret the statement given by willyw in his original post :-<br><div class="codebox"><p>Code: </p><pre><code>puts $fp list [array get $arr_name] </code></pre></div>On reflection, I'm surprised the above statement worked at all. I would have thought an error would result.<p>Statistics: Posted by <a href="https://forum.eggheads.org/memberlist.php?mode=viewprofile&amp;u=5705">arfer</a> — Fri Oct 30, 2009 2:50 am</p><hr />
]]></content>
	</entry>
		<entry>
		<author><name><![CDATA[nml375]]></name></author>
		<updated>2009-10-29T14:10:55-04:00</updated>

		<published>2009-10-29T14:10:55-04:00</published>
		<id>https://forum.eggheads.org/viewtopic.php?p=90673#p90673</id>
		<link href="https://forum.eggheads.org/viewtopic.php?p=90673#p90673"/>
		<title type="html"><![CDATA[Save array to file /  restore array from file]]></title>

		
		<content type="html" xml:base="https://forum.eggheads.org/viewtopic.php?p=90673#p90673"><![CDATA[
Indeed, fixed that flaw just a minute ago.<br><br>Double evaluation is the condition when a piece of code is passed to the interpreter twice. This results in variable and command substitutions being done twice. One classic example is the utimer command:<div class="codebox"><p>Code: </p><pre><code>proc join_greet {nick host hand chan} {  utimer 10 "puthelp \"PRIVMSG $chan :Hello $nick\""}</code></pre></div>Assume the above code is linked to a join-binding; once a user joins the channel, the code is evaluated and a 10 second timer is started. The command for the timer would be this:<div class="codebox"><p>Code: </p><pre><code>puthelp "PRIVMSG #thechannel :Hello Thenick"</code></pre></div>Which looks pretty safe... However, if someone uses the nick I[die] when joining, that would cause a command substitution (due to the []), executing the command "die", thus killing your eggdrop.<br>The proper way to do a script like that is like this:<div class="codebox"><p>Code: </p><pre><code>proc join_greet {nick host hand chan] { utimer 10 [list puthelp "PRIVMSG $chan :Hello $nick"]}</code></pre></div>Lists have the feature of preserving it's content through command and variable substitutions, as well as preventing "data bleeding" (a string with spaces being treated as two or more arguments).<br><br>There are many other occasions where double evaluation comes into play, in my code, the second evaluation would be when you load the datafile as a script.<br><br>One major concern with arfer's current code, is that things will break badly if either any array indexname or value contains newlines, or if the indexname contains spaces. This is an unfortunate side-effect from using split.<p>Statistics: Posted by <a href="https://forum.eggheads.org/memberlist.php?mode=viewprofile&amp;u=8052">nml375</a> — Thu Oct 29, 2009 2:10 pm</p><hr />
]]></content>
	</entry>
		<entry>
		<author><name><![CDATA[willyw]]></name></author>
		<updated>2009-10-29T13:58:35-04:00</updated>

		<published>2009-10-29T13:58:35-04:00</published>
		<id>https://forum.eggheads.org/viewtopic.php?p=90672#p90672</id>
		<link href="https://forum.eggheads.org/viewtopic.php?p=90672#p90672"/>
		<title type="html"><![CDATA[Save array to file /  restore array from file]]></title>

		
		<content type="html" xml:base="https://forum.eggheads.org/viewtopic.php?p=90672#p90672"><![CDATA[
<blockquote class="uncited"><div>Also, may I suggest an improved version of arfer's approach:<br><br>The changes in the code above should guarantee data remaining intact even if they should contain newlines or other "awkward" characters...<br><br>Edit: doing something wrong in my logics... will get back with working code...</div></blockquote>You have my attention.    <img class="smilies" src="https://forum.eggheads.org/images/smilies/icon_smile.gif" width="15" height="15" alt=":)" title="Smile"><br>I'll be checking back often.<br><br><br>Thank you.<p>Statistics: Posted by <a href="https://forum.eggheads.org/memberlist.php?mode=viewprofile&amp;u=10420">willyw</a> — Thu Oct 29, 2009 1:58 pm</p><hr />
]]></content>
	</entry>
		<entry>
		<author><name><![CDATA[willyw]]></name></author>
		<updated>2009-10-29T13:56:22-04:00</updated>

		<published>2009-10-29T13:56:22-04:00</published>
		<id>https://forum.eggheads.org/viewtopic.php?p=90671#p90671</id>
		<link href="https://forum.eggheads.org/viewtopic.php?p=90671#p90671"/>
		<title type="html"><![CDATA[Save array to file /  restore array from file]]></title>

		
		<content type="html" xml:base="https://forum.eggheads.org/viewtopic.php?p=90671#p90671"><![CDATA[
Thanks for replying.<br><blockquote class="uncited"><div>Just to pitch in;<br>The list command used in the first linked thread is not used to "protect" the output from <strong class="text-strong">array get</strong>. In fact, the list command will never see that output, instead it is used to avoid "double evaluation" when generating script code on the fly.</div></blockquote>Sorry, I don't understand what double evaluation means. <br><br><blockquote class="uncited"><div>The most trivial form would be something as simple as this. In fact, it's a very stripped down version of the script posted by <em class="text-italics">user</em> in that thread, with no checks for the existance or type of the variable/array:<div class="codebox"><p>Code: </p><pre><code>proc saveArray {varname file} { upvar 1 $varname v set fp [open $file w] puts $fp [list array set [array get $v]] close $fp}</code></pre></div>This would then be restored by simply loading the generated file as a script (source thefile).</div></blockquote>I played with this.   Just to write out a file... did not try reading the file back.<br><br>I see what happens.   Interesting.    <img class="smilies" src="https://forum.eggheads.org/images/smilies/icon_smile.gif" width="15" height="15" alt=":)" title="Smile"><br>But wouldn't one have to   [list array set array_name [array get $v]]<br>though?      I'm saying this, after looking at what was written to the file.<br><br>No offense, but I think I want to continue with the other way...  reading a file, not sourcing it.    Only because I started down that path.   <br>I've no idea if one method is better than the other.    <br><br>All this stuff is very interesting to learn, and I'm glad you posted that.  I sort of glossed over it, when I'd read user's original posts.<br><br>Thanks<p>Statistics: Posted by <a href="https://forum.eggheads.org/memberlist.php?mode=viewprofile&amp;u=10420">willyw</a> — Thu Oct 29, 2009 1:56 pm</p><hr />
]]></content>
	</entry>
		<entry>
		<author><name><![CDATA[nml375]]></name></author>
		<updated>2009-10-29T13:29:38-04:00</updated>

		<published>2009-10-29T13:29:38-04:00</published>
		<id>https://forum.eggheads.org/viewtopic.php?p=90670#p90670</id>
		<link href="https://forum.eggheads.org/viewtopic.php?p=90670#p90670"/>
		<title type="html"><![CDATA[Save array to file /  restore array from file]]></title>

		
		<content type="html" xml:base="https://forum.eggheads.org/viewtopic.php?p=90670#p90670"><![CDATA[
Also, may I suggest an improved version of arfer's approach:<br><br>The changes in the code above should guarantee data remaining intact even if they should contain newlines or other "awkward" characters...<br><br>Edit: doing something wrong in my logics... will get back with working code...<p>Statistics: Posted by <a href="https://forum.eggheads.org/memberlist.php?mode=viewprofile&amp;u=8052">nml375</a> — Thu Oct 29, 2009 1:29 pm</p><hr />
]]></content>
	</entry>
		<entry>
		<author><name><![CDATA[willyw]]></name></author>
		<updated>2009-10-29T13:15:04-04:00</updated>

		<published>2009-10-29T13:15:04-04:00</published>
		<id>https://forum.eggheads.org/viewtopic.php?p=90669#p90669</id>
		<link href="https://forum.eggheads.org/viewtopic.php?p=90669#p90669"/>
		<title type="html"><![CDATA[Save array to file /  restore array from file]]></title>

		
		<content type="html" xml:base="https://forum.eggheads.org/viewtopic.php?p=90669#p90669"><![CDATA[
<blockquote class="uncited"><div>Sorry, my mistake. </div></blockquote>Not at all.   Mine.   I meant to mention ....<br><blockquote class="uncited"><div>I should have grouped together the arguments in the 'puts' command by placing them inside quotes.</div></blockquote>...  that I'd tried that.<br>Didn't work.<br>I remember trying      puts $id $name "$value"<br>and I would have sworn I then tried     puts $id "$name $value"  , just like yours below.<br>And it still didn't work.      ...  I thought.<br><br>But, I went ahead and did it again, today.<br><br>One brief trial today, and it seems to work fine.<br>Apparently I was half asleep....<br><blockquote class="uncited"><div><div class="codebox"><p>Code: </p><pre><code>proc pWriteArray {} {  global vDataArray  set id [open whatever.txt w]  foreach {name value} [array get vDataArray] {    puts $id "$name $value"  }  close $id  return 0}</code></pre></div>Don't forget that I said the code is potentially incomplete. Eg. What do you want to happen if pWriteArray is called and vDataArray does not exist? The code is unfinished until you can account for every eventuality.</div></blockquote>Understand your concern/warning.    Thanks.   <img class="smilies" src="https://forum.eggheads.org/images/smilies/icon_smile.gif" width="15" height="15" alt=":)" title="Smile"><br><br>Let me get back to building with what you've given me, and see what I can do.   I needed this basic method to get me over this hump.<br><br>Still not sure if the style that I started with would have been ok to use, in the way I described that I'd gotten it working.<br>But no matter now.<br>I like the way the file looks, much better -  with your method.<br>The pairs on each on their own line,   not just one long line.<br>Not that I plan to ever want to examine the file with my own eyes,  but if I ever should, it is certainly much nicer, when using your method to write it.<br><br><br><br>Thank you<p>Statistics: Posted by <a href="https://forum.eggheads.org/memberlist.php?mode=viewprofile&amp;u=10420">willyw</a> — Thu Oct 29, 2009 1:15 pm</p><hr />
]]></content>
	</entry>
		<entry>
		<author><name><![CDATA[nml375]]></name></author>
		<updated>2009-10-30T13:29:45-04:00</updated>

		<published>2009-10-29T12:55:47-04:00</published>
		<id>https://forum.eggheads.org/viewtopic.php?p=90668#p90668</id>
		<link href="https://forum.eggheads.org/viewtopic.php?p=90668#p90668"/>
		<title type="html"><![CDATA[Save array to file /  restore array from file]]></title>

		
		<content type="html" xml:base="https://forum.eggheads.org/viewtopic.php?p=90668#p90668"><![CDATA[
Just to pitch in;<br>The list command used in the first linked thread is not used to "protect" the output from <strong class="text-strong">array get</strong>. In fact, the list command will never see that output, instead it is used to avoid "double evaluation" when generating script code on the fly.<br><br>The most trivial form would be something as simple as this. In fact, it's a very stripped down version of the script posted by <em class="text-italics">user</em> in that thread, with no checks for the existance or type of the variable/array:<div class="codebox"><p>Code: </p><pre><code>proc saveArray {varname file} { upvar 1 $varname v set fp [open $file w] puts $fp [list array set $varname [array get v]] close $fp}</code></pre></div>This would then be restored by simply loading the generated file as a script (source thefile).<br><br>Edit: Forgot to include the variable name in array set... fixed.<br>Edit: used $v instead of v... fixed.<p>Statistics: Posted by <a href="https://forum.eggheads.org/memberlist.php?mode=viewprofile&amp;u=8052">nml375</a> — Thu Oct 29, 2009 12:55 pm</p><hr />
]]></content>
	</entry>
		<entry>
		<author><name><![CDATA[arfer]]></name></author>
		<updated>2009-10-29T06:22:03-04:00</updated>

		<published>2009-10-29T06:22:03-04:00</published>
		<id>https://forum.eggheads.org/viewtopic.php?p=90665#p90665</id>
		<link href="https://forum.eggheads.org/viewtopic.php?p=90665#p90665"/>
		<title type="html"><![CDATA[Save array to file /  restore array from file]]></title>

		
		<content type="html" xml:base="https://forum.eggheads.org/viewtopic.php?p=90665#p90665"><![CDATA[
Sorry, my mistake. I should have grouped together the arguments in the 'puts' command by placing them inside quotes.<br><div class="codebox"><p>Code: </p><pre><code>proc pWriteArray {} {  global vDataArray  set id [open whatever.txt w]  foreach {name value} [array get vDataArray] {    puts $id "$name $value"  }  close $id  return 0}</code></pre></div>Don't forget that I said the code is potentially incomplete. Eg. What do you want to happen if pWriteArray is called and vDataArray does not exist? The code is unfinished until you can account for every eventuality.<p>Statistics: Posted by <a href="https://forum.eggheads.org/memberlist.php?mode=viewprofile&amp;u=5705">arfer</a> — Thu Oct 29, 2009 6:22 am</p><hr />
]]></content>
	</entry>
		<entry>
		<author><name><![CDATA[willyw]]></name></author>
		<updated>2009-10-29T01:15:14-04:00</updated>

		<published>2009-10-29T01:15:14-04:00</published>
		<id>https://forum.eggheads.org/viewtopic.php?p=90664#p90664</id>
		<link href="https://forum.eggheads.org/viewtopic.php?p=90664#p90664"/>
		<title type="html"><![CDATA[Save array to file /  restore array from file]]></title>

		
		<content type="html" xml:base="https://forum.eggheads.org/viewtopic.php?p=90664#p90664"><![CDATA[
Thanks for replying.<br><blockquote class="uncited"><div>...<br> Similar difficulties would occur if one or more element name contains spaces (definitely not adviseable)</div></blockquote>That is even possible??   that's interesting.<br><br>But, in this case -  no.   The element names do not ever contain spaces.<br><br><blockquote class="uncited"><div> or if one or more element value contains spaces. </div></blockquote>They do.    Random length too.   Any value could be a complete sentence, for example.<br><br>someword,1 Here could be any random sentence, ... anything at all<br>someword,2 including any sort of punctuation<br>someword,3 and so on and so on <br><br><blockquote class="uncited"><div>In those cases the saved text may not be an even number of 'words' representing name/value pairs and so would not readily read back into an array variable.</div></blockquote>Exactly.<br><blockquote class="uncited"><div>If the array element names do not contain spaces (as should normally be the case) and no array element value is an empty string (though only minor modification is needed to deal with this), </div></blockquote>I hadn't thought of that possibility... but yes,  I suppose it would be possible for a value to to be empty.<br><blockquote class="uncited"><div>I would probably choose to save the data to a file as one array element per line. The first word in the line being the array element name and the remainder being the array element value. Something like the following (untested) :-<br><div class="codebox"><p>Code: </p><pre><code>proc pReadArray {} {  global vDataArray  set id [open whatever.txt r]  set data [split [read -nonewline $id] \n]  foreach item $data {    set name [lindex [split $item] 0]    set value [join [lrange [split $item] 1 end]]    set vDataArray($name) $value  }  close $id  return 0}proc pWriteArray {} {  global vDataArray  set id [open whatever.txt w]  foreach {name value} [array get vDataArray] {    puts $id $name $value  }  close $id  return 0}</code></pre></div>You would obviously call pReadArray to read in the data and populate the array vDataArray, and call pWriteArray to save the vDataArray name/value pairs to a text file.<br><br>Depending on exactly what the array names/values could contain, you might have to rethink things.<br><br>The code above is probably incomplete, even if it is correct for your needs. For example you might wish to test if the file exists before trying to read it etc etc.</div></blockquote>Tried it (writing to file) quickly, and it didn't work.  It had to do with $value being more than one word, I believe.<br><br>Played around with trying to solve that a little bit.<br>Tried          puts $id [list $name $value]<br>and it produces a nice looking file.      value is inside curly braces.<br>But...  reading that file back,   and value now is inside double curly braces.<br><br>Decided not to fiddle with it more right now.   Late, and tired.  Waste of time.   <img class="smilies" src="https://forum.eggheads.org/images/smilies/icon_smile.gif" width="15" height="15" alt=":)" title="Smile">       Will tackle it tomorrow, after a night's sleep.<br><br>Just wanted to get back to you with this info,  in case you might be able to direct me with method, - and commands I'll need to read up on.<br><br>Thanks<p>Statistics: Posted by <a href="https://forum.eggheads.org/memberlist.php?mode=viewprofile&amp;u=10420">willyw</a> — Thu Oct 29, 2009 1:15 am</p><hr />
]]></content>
	</entry>
		<entry>
		<author><name><![CDATA[arfer]]></name></author>
		<updated>2009-10-28T21:45:32-04:00</updated>

		<published>2009-10-28T21:45:32-04:00</published>
		<id>https://forum.eggheads.org/viewtopic.php?p=90663#p90663</id>
		<link href="https://forum.eggheads.org/viewtopic.php?p=90663#p90663"/>
		<title type="html"><![CDATA[Save array to file /  restore array from file]]></title>

		
		<content type="html" xml:base="https://forum.eggheads.org/viewtopic.php?p=90663#p90663"><![CDATA[
The command 'array get' already returns a list consisting of pairs of element names/values, so at first sight the use of an additional list command seems superfluous and causes the data to be saved with the curly braces.<br><br>The only thing I can think the author is doing is trying to preserve the data when reading it back into an array if, for example, one or more of the element values is an empty string. What you have to consider is that data saved to a file is neither an array or a list because it is then outside of a Tcl script. It is merely a line of text in a file. Similar difficulties would occur if one or more element name contains spaces (definitely not adviseable) or if one or more element value contains spaces. In those cases the saved text may not be an even number of 'words' representing name/value pairs and so would not readily read back into an array variable.<br><br>If the array element names do not contain spaces (as should normally be the case) and no array element value is an empty string (though only minor modification is needed to deal with this), I would probably choose to save the data to a file as one array element per line. The first word in the line being the array element name and the remainder being the array element value. Something like the following (untested) :-<br><div class="codebox"><p>Code: </p><pre><code>proc pReadArray {} {  global vDataArray  set id [open whatever.txt r]  set data [split [read -nonewline $id] \n]  foreach item $data {    set name [lindex [split $item] 0]    set value [join [lrange [split $item] 1 end]]    set vDataArray($name) $value  }  close $id  return 0}proc pWriteArray {} {  global vDataArray  set id [open whatever.txt w]  foreach {name value} [array get vDataArray] {    puts $id $name $value  }  close $id  return 0}</code></pre></div>You would obviously call pReadArray to read in the data and populate the array vDataArray, and call pWriteArray to save the vDataArray name/value pairs to a text file.<br><br>Depending on exactly what the array names/values could contain, you might have to rethink things.<br><br>The code above is probably incomplete, even if it is correct for your needs. For example you might wish to test if the file exists before trying to read it etc etc.<p>Statistics: Posted by <a href="https://forum.eggheads.org/memberlist.php?mode=viewprofile&amp;u=5705">arfer</a> — Wed Oct 28, 2009 9:45 pm</p><hr />
]]></content>
	</entry>
	</feed>
