<?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/6885" />

	<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>2010-09-06T16:39:45-04:00</updated>

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

		<entry>
		<author><name><![CDATA[username]]></name></author>
		<updated>2010-09-06T16:39:45-04:00</updated>

		<published>2010-09-06T16:39:45-04:00</published>
		<id>https://forum.eggheads.org/viewtopic.php?p=94226#p94226</id>
		<link href="https://forum.eggheads.org/viewtopic.php?p=94226#p94226"/>
		<title type="html"><![CDATA[Re: Basic File Operations - Evaluating variables read from f]]></title>

		
		<content type="html" xml:base="https://forum.eggheads.org/viewtopic.php?p=94226#p94226"><![CDATA[
<blockquote class="uncited"><div>If I have variables, such as $nick or $arg, in the file, it doesn't evaluate the variables. Instead, it just leaves it at $nick or $arg.<br>Is there any way to evaluate the variables?<br>using [eval] makes Tcl want to use the first word of the line as a command, which is not appropriate when I have, say:<br>putmsg $chan "[eval [readFile filename.txt]]"<br>(readFile is a seperate process that reads the file and returns a random line)</div></blockquote>You need to use <a href="http://www.tcl.tk/man/tcl8.5/TclCmd/subst.htm" class="postlink"><strong class="text-strong">subst</strong></a> command. Look at this example: <div class="codebox"><p>Code: </p><pre><code>        set realmsgs {         "Hi, how are you?"         "Hi $nick!"         "Hello $nick, welcome to $chan"         "]]] WELCOME to $chan!!!11111 ^^ ^^ [[["         "Hi [$nick], you have to pay 20$ to enter here"     }          bind join - * greet     proc greet {nick host hand chan} {         set greetmsg [lindex $::realmsgs [rand [llength $::realmsgs]]]         set greetmsg [subst -nocommands $greetmsg]         putserv "PRIVMSG $nick :$greetmsg"     }</code></pre></div><p>Statistics: Posted by <a href="https://forum.eggheads.org/memberlist.php?mode=viewprofile&amp;u=6813">username</a> — Mon Sep 06, 2010 4:39 pm</p><hr />
]]></content>
	</entry>
		<entry>
		<author><name><![CDATA[A Hylian Human]]></name></author>
		<updated>2010-09-06T12:03:19-04:00</updated>

		<published>2010-09-06T12:03:19-04:00</published>
		<id>https://forum.eggheads.org/viewtopic.php?p=94223#p94223</id>
		<link href="https://forum.eggheads.org/viewtopic.php?p=94223#p94223"/>
		<title type="html"><![CDATA[Basic File Operations - Evaluating variables read from file]]></title>

		
		<content type="html" xml:base="https://forum.eggheads.org/viewtopic.php?p=94223#p94223"><![CDATA[
If I have variables, such as $nick or $arg, in the file, it doesn't evaluate the variables. Instead, it just leaves it at $nick or $arg.<br>Is there any way to evaluate the variables?<br>using [eval] makes Tcl want to use the first word of the line as a command, which is not appropriate when I have, say:<br>putmsg $chan "[eval [readFile filename.txt]]"<br>(readFile is a seperate process that reads the file and returns a random line)<p>Statistics: Posted by <a href="https://forum.eggheads.org/memberlist.php?mode=viewprofile&amp;u=11346">A Hylian Human</a> — Mon Sep 06, 2010 12:03 pm</p><hr />
]]></content>
	</entry>
		<entry>
		<author><name><![CDATA[SL0RD]]></name></author>
		<updated>2008-10-28T17:54:42-04:00</updated>

		<published>2008-10-28T17:54:42-04:00</published>
		<id>https://forum.eggheads.org/viewtopic.php?p=85676#p85676</id>
		<link href="https://forum.eggheads.org/viewtopic.php?p=85676#p85676"/>
		<title type="html"><![CDATA[Basic File Operations]]></title>

		
		<content type="html" xml:base="https://forum.eggheads.org/viewtopic.php?p=85676#p85676"><![CDATA[
Thanks for the great basic tutorial.<br><br>I have two "requests"<br><br>First off, say you had this:<div class="codebox"><p>Code: </p><pre><code>thing1 discthing2 discthing3 discthing4 disc</code></pre></div>I would like to add a new line but if the line that i want to add starts with thing1 for example instead of adding it again at the end i want to update the existing thing1<br>also how could you delete a line by using lsearch but by searching the first word in each line so i would like to delete things3 for example by searching for thing3<br><br>secondly, say you had that same table, I want to be able to do a command and have it pull the first word from every line and list it in the channel ex: &lt;bot&gt; thing1, thing2, thing3, thing4<br><br>That is all for now but i will probably have more questions later. thanks <img class="smilies" src="https://forum.eggheads.org/images/smilies/icon_wink.gif" width="15" height="15" alt=";)" title="Wink"><p>Statistics: Posted by <a href="https://forum.eggheads.org/memberlist.php?mode=viewprofile&amp;u=10253">SL0RD</a> — Tue Oct 28, 2008 5:54 pm</p><hr />
]]></content>
	</entry>
		<entry>
		<author><name><![CDATA[user]]></name></author>
		<updated>2007-01-17T10:37:42-04:00</updated>

		<published>2007-01-17T10:37:42-04:00</published>
		<id>https://forum.eggheads.org/viewtopic.php?p=69677#p69677</id>
		<link href="https://forum.eggheads.org/viewtopic.php?p=69677#p69677"/>
		<title type="html"><![CDATA[Re: Basic File Operations]]></title>

		
		<content type="html" xml:base="https://forum.eggheads.org/viewtopic.php?p=69677#p69677"><![CDATA[
<a href="http://tcl.tk/man/tcl8.3/TclCmd/lsearch.htm" class="postlink">lsearch</a><br><div class="codebox"><p>Code: </p><pre><code># delete ALL lines ending with " user8"set match "* user8"while {[set i [lsearch -glob $lines $match]]&gt;-1} {set lines [lreplace $lines $i $i]}</code></pre></div><p>Statistics: Posted by <a href="https://forum.eggheads.org/memberlist.php?mode=viewprofile&amp;u=2878">user</a> — Wed Jan 17, 2007 10:37 am</p><hr />
]]></content>
	</entry>
		<entry>
		<author><name><![CDATA[Gust]]></name></author>
		<updated>2007-01-17T10:09:41-04:00</updated>

		<published>2007-01-17T10:09:41-04:00</published>
		<id>https://forum.eggheads.org/viewtopic.php?p=69675#p69675</id>
		<link href="https://forum.eggheads.org/viewtopic.php?p=69675#p69675"/>
		<title type="html"><![CDATA[Re: Basic File Operations]]></title>

		
		<content type="html" xml:base="https://forum.eggheads.org/viewtopic.php?p=69675#p69675"><![CDATA[
<blockquote class="uncited"><div><strong class="text-strong">3. Deleting a line from a text file.</strong><br>We'll assume you want to delete a specific line. Remember that most languages, including tcl, index things starting from 0. So if you want to delete the first line, it is line 0. The second line is line 1. Etc.</div></blockquote>First of all, thanks for this explanation.<br><br>In the piece of code you've provided, you should allready know the line-number of the line you want to remove...<br>But what if you don't know the line-number in the textfile?<br><br>For example: I have a file that contains the following:<div class="codebox"><p>Code: </p><pre><code>hostname 1 user1hostname5 user5hostname8 user8hostname3 user3hostname12 user12hostname6 user6</code></pre></div>And i want to delete the following line:<div class="codebox"><p>Code: </p><pre><code>hostname8 user8</code></pre></div>by searching on "user8"; how can i do that?<br><br>Thanks in advance!<br><br>Greetings, <br>Gust<p>Statistics: Posted by <a href="https://forum.eggheads.org/memberlist.php?mode=viewprofile&amp;u=7913">Gust</a> — Wed Jan 17, 2007 10:09 am</p><hr />
]]></content>
	</entry>
		<entry>
		<author><name><![CDATA[strikelight]]></name></author>
		<updated>2004-02-18T23:25:02-04:00</updated>

		<published>2004-02-18T23:25:02-04:00</published>
		<id>https://forum.eggheads.org/viewtopic.php?p=33726#p33726</id>
		<link href="https://forum.eggheads.org/viewtopic.php?p=33726#p33726"/>
		<title type="html"><![CDATA[Re: Basic File Operations]]></title>

		
		<content type="html" xml:base="https://forum.eggheads.org/viewtopic.php?p=33726#p33726"><![CDATA[
<blockquote class="uncited"><div><strong class="text-strong">3. Deleting a line from a text file.</strong><br>We'll assume you want to delete a specific line. Remember that most languages, including tcl, index things starting from 0. So if you want to delete the first line, it is line 0. The second line is line 1. Etc.<br><div class="codebox"><p>Code: </p><pre><code># Use the code from above (1.) to read in all the lines from the file.# We continue right after: set lines [split $data "\n"]# We'll delete the first line.set line_to_delete 0# If you wanted to delete the last line instead, you would do this:# set line_to_delete [expr [llength $lines] - 1]# Now, we remove the line from the list in memory first.set lines [lreplace $lines $line_to_delete $line_to_delete]# And finally, we re-write the file with the new data.set fp [open $fp "w"]puts $fp [join $lines "\n"]close $fp</code></pre></div>As you can see, to delete a line from a file, we have to read the file into memory, remove the line from memory, then overwrite the file on disk. If you are doing a lot of adding/removing of records, you should consider using a database instead! Also, if your file is very large (several megabytes), these operations will take some time -- you should not be using a flat text file.</div></blockquote>Just a couple of optimization tips, especially with reference to this section:<br>Take advantage of TCL's expression optimization by using {}'s:<div class="codebox"><p>Code: </p><pre><code># If you wanted to delete the last line instead, you would do this:# set line_to_delete [expr [llength $lines] - 1]to:# set line_to_delete [expr {[llength $lines] - 1}]</code></pre></div>When performing an in-place lreplace (that is to say, setting a variable to the result of an lreplace operation on the same variable itself), performance can be noticably enhanced by using the following:<br><div class="codebox"><p>Code: </p><pre><code># Now, we remove the line from the list in memory first.set lines [lreplace $lines $line_to_delete $line_to_delete]Becomes:proc K {x y} { set x }set lines [lreplace [K $lines [set lines {}]] $line_to_delete $line_to_delete]</code></pre></div>(Tips obtained from <a href="http://mini.net/tcl" class="postlink">The Tcl'ers Wiki</a>).<p>Statistics: Posted by <a href="https://forum.eggheads.org/memberlist.php?mode=viewprofile&amp;u=2005">strikelight</a> — Wed Feb 18, 2004 11:25 pm</p><hr />
]]></content>
	</entry>
		<entry>
		<author><name><![CDATA[stdragon]]></name></author>
		<updated>2004-02-18T18:57:38-04:00</updated>

		<published>2004-02-18T18:57:38-04:00</published>
		<id>https://forum.eggheads.org/viewtopic.php?p=33712#p33712</id>
		<link href="https://forum.eggheads.org/viewtopic.php?p=33712#p33712"/>
		<title type="html"><![CDATA[Basic File Operations]]></title>

		
		<content type="html" xml:base="https://forum.eggheads.org/viewtopic.php?p=33712#p33712"><![CDATA[
There are lots of questions about reading lines from files, replacing lines, etc. So I'll address a few things here.<br><br><strong class="text-strong">1. Reading all the lines from a file into a list.</strong><br>This is one of the most basic things you can do. It's the basis for several other operations we'll be discussing, so get familiar with it!<br><div class="codebox"><p>Code: </p><pre><code># File name to read.set fname "yourfile.txt"# Open file for read access (note we're not catching errors, you might# want to use catch {} if the file might not exist.set fp [open $fname "r"]# Here we read in all of the data.set data [read -nonewline $fp]# Close the file, since we're done reading.close $fp# Now we split the data into lines.set lines [split $data "\n"]</code></pre></div>There you go, a list of all the lines in the file, with just a few simple lines of code! On to more advanced topics.<br><br><strong class="text-strong">2. Reading a random line from a (small) file.</strong><br>There are lots of reasons you might want to do this: quote scripts, random topic scripts, trivia games, etc. In this example we'll only be reading a single line, but you could easily change it to read more than 1 line in a row, or multiple lines from different places.<br><div class="codebox"><p>Code: </p><pre><code># Use the code from above (1.) to read in all the lines from the file.# We continue right after: set lines [split $data "\n"]# Get the number of lines.set numlines [llength $lines]# Choose a random line with eggdrop's rand function.set num [rand $numlines]# Get the line from the list!set randline [lindex $lines $num]</code></pre></div>Now you have the random line in the $randline variable. If you wanted to read multiple lines, you could put the last part in a loop.<br><br><strong class="text-strong">3. Deleting a line from a text file.</strong><br>We'll assume you want to delete a specific line. Remember that most languages, including tcl, index things starting from 0. So if you want to delete the first line, it is line 0. The second line is line 1. Etc.<br><div class="codebox"><p>Code: </p><pre><code># Use the code from above (1.) to read in all the lines from the file.# We continue right after: set lines [split $data "\n"]# We'll delete the first line.set line_to_delete 0# If you wanted to delete the last line instead, you would do this:# set line_to_delete [expr [llength $lines] - 1]# Now, we remove the line from the list in memory first.set lines [lreplace $lines $line_to_delete $line_to_delete]# And finally, we re-write the file with the new data.set fp [open $fp "w"]puts $fp [join $lines "\n"]close $fp</code></pre></div>As you can see, to delete a line from a file, we have to read the file into memory, remove the line from memory, then overwrite the file on disk. If you are doing a lot of adding/removing of records, you should consider using a database instead! Also, if your file is very large (several megabytes), these operations will take some time -- you should not be using a flat text file.<br><br><strong class="text-strong">4. Inserting a line into a file.</strong><br>Usually people want to insert lines at the beginning of a file, or append them onto the end. The latter will be handled in the next question.<br><div class="codebox"><p>Code: </p><pre><code># Use the code from above (1.) to read in all the lines from the file.# We continue right after: set lines [split $data "\n"]# For the beginning, use line 0, since tcl starts counting at 0. If you want# to append the line to the end, see the next question for a better way.set line_to_insert "this is the new line"set where_to_insert 0# Now we insert the line into our list in memory.set lines [linsert $lines $where_to_insert $line_to_insert]# And now we re-write the file, just like in 3.set fp [open $fp "w"]puts $fp [join $lines "\n"]close $fp</code></pre></div><strong class="text-strong">5. Add a line to the end of a file.</strong><br>For the special case of adding a line to the end of the file, we don't need<br>to mess with reading/re-writing at all. We simply open the file in a special<br>mode called "append mode."<br><div class="codebox"><p>Code: </p><pre><code># The new line.set line_to_add "the dragon will eat your sheep!"# Name of file to append to.set fname "yourfile.txt"# Open the file in append mode.set fp [open $fname "a"]# Add the line.puts $fp $line_to_add# We're done!close $fp</code></pre></div><br><br><strong class="text-strong">If you'd like to see some other file i/o topics answered here, make a post below!</strong><br>Of course, don't bother with things like "how do I read a random line and display it in a private message" because the whole reading-random-lines topic is covered already.<p>Statistics: Posted by <a href="https://forum.eggheads.org/memberlist.php?mode=viewprofile&amp;u=8">stdragon</a> — Wed Feb 18, 2004 6:57 pm</p><hr />
]]></content>
	</entry>
	</feed>
