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

	<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>2015-03-13T11:59:53-04:00</updated>

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

		<entry>
		<author><name><![CDATA[Elfriede]]></name></author>
		<updated>2015-03-13T11:59:53-04:00</updated>

		<published>2015-03-13T11:59:53-04:00</published>
		<id>https://forum.eggheads.org/viewtopic.php?p=103573#p103573</id>
		<link href="https://forum.eggheads.org/viewtopic.php?p=103573#p103573"/>
		<title type="html"><![CDATA[Compare text and output match in percentage ?]]></title>

		
		<content type="html" xml:base="https://forum.eggheads.org/viewtopic.php?p=103573#p103573"><![CDATA[
wow thanks, ill give it a try<p>Statistics: Posted by <a href="https://forum.eggheads.org/memberlist.php?mode=viewprofile&amp;u=9204">Elfriede</a> — Fri Mar 13, 2015 11:59 am</p><hr />
]]></content>
	</entry>
		<entry>
		<author><name><![CDATA[caesar]]></name></author>
		<updated>2015-03-10T08:15:46-04:00</updated>

		<published>2015-03-10T08:15:46-04:00</published>
		<id>https://forum.eggheads.org/viewtopic.php?p=103567#p103567</id>
		<link href="https://forum.eggheads.org/viewtopic.php?p=103567#p103567"/>
		<title type="html"><![CDATA[Compare text and output match in percentage ?]]></title>

		
		<content type="html" xml:base="https://forum.eggheads.org/viewtopic.php?p=103567#p103567"><![CDATA[
There's no straight forward functions that you can use to get such a percentage, but different functions to help you achieve that.<br><br>If the two strings have words separated by a dot, comma or just space you would need to turn the string into a list by splitting them by that element, in your case that dot. We achieve this by using the <em class="text-italics">split</em> function.<div class="codebox"><p>Code: </p><pre><code>set texta [split $texta .]set textb [split $textb .]</code></pre></div>Next to to see how many elements of the first list (texta) are in the second (textb) you have to loop tough all the elements of the list and check if they are present in the second.<br><br>We achieve this by using <em class="text-italics">foreach</em> to loop, <em class="text-italics">lsearch</em> to see if a element is in a list and by using <em class="text-italics">incr</em> will be incrementing the <em class="text-italics">count</em> variable when a match was found.<div class="codebox"><p>Code: </p><pre><code>set count 0foreach ele $texta {if {[lsearch -nocase $textb $ele] != -1} {incr count}}</code></pre></div>The <em class="text-italics">-nocase</em> option tells the lsearch function to ignore the lower, upper or mixed case of the characters of the "element" (or simply put word) when doing the match search.<br><br>After the <em class="text-italics">foreach</em> loop has finished you will have in $count the total number of matches it found. Notice the lack of $ in front of the count at the incr line. There's no need for it there, but when you want to see it's value you would need that dollar sign in front of it.<br><br>If you want to see how long is the second list and how many of the words it matched then you could use <em class="text-italics">llength</em> to get it's length and expr to do the math calculation of the exact percentage.<div class="codebox"><p>Code: </p><pre><code>set size [llength $textb]set percentage [expr $count*100/$size]</code></pre></div>So now in $percentage you got exactly how much the first string matches the second, in your case it's a 75% match.<br><br>Something worth checking: <a href="http://zetcode.com/lang/tcl/lists/" class="postlink">lists</a> and <a href="http://zetcode.com/lang/tcl/strings/" class="postlink">strings</a><br><br><strong class="text-strong">Edit:</strong> After a bit of thinking I've come with a different approach that's a bit more straight forward than the above method by using <em class="text-italics">lmap</em>, but would still need to split those two strings. <img class="smilies" src="https://forum.eggheads.org/images/smilies/icon_smile.gif" width="15" height="15" alt=":)" title="Smile"><div class="codebox"><p>Code: </p><pre><code>set texta [split $texta .]set textb [split $textb .]set matches [lmap x $texta {expr {[lsearch -nocase $textb $x] !=1 ? $x : [continue]}}]</code></pre></div>Now $matches would contain the elements that are common to the two strings and from here can just get the size of the second list (after splitting the second string) and the size of the matches list then do the math to get percentage.<div class="codebox"><p>Code: </p><pre><code>set size [llength $textb]set count [llength $matches]set percentage [expr $count*100/$size]</code></pre></div>If you want to stick with the first approach and get the matches inside a variable then we add the <em class="text-italics">lappend</em> to the <em class="text-italics">foreach</em> loop list this.<div class="codebox"><p>Code: </p><pre><code>set matches [list]foreach ele $texta {if {[lsearch -nocase $textb $ele] != -1} {incr countlappend matches $ele   }}</code></pre></div>Notice that we first need to define the matches variable prior to using it, hence the line on top of the start of the <em class="text-italics">foreach</em> loop.<br><br>If you got any questions don't hesitate to ask. <img class="smilies" src="https://forum.eggheads.org/images/smilies/icon_wink.gif" width="15" height="15" alt=":wink:" title="Wink"><p>Statistics: Posted by <a href="https://forum.eggheads.org/memberlist.php?mode=viewprofile&amp;u=187">caesar</a> — Tue Mar 10, 2015 8:15 am</p><hr />
]]></content>
	</entry>
		<entry>
		<author><name><![CDATA[Elfriede]]></name></author>
		<updated>2015-03-10T07:02:30-04:00</updated>

		<published>2015-03-10T07:02:30-04:00</published>
		<id>https://forum.eggheads.org/viewtopic.php?p=103566#p103566</id>
		<link href="https://forum.eggheads.org/viewtopic.php?p=103566#p103566"/>
		<title type="html"><![CDATA[Compare text and output match in percentage ?]]></title>

		
		<content type="html" xml:base="https://forum.eggheads.org/viewtopic.php?p=103566#p103566"><![CDATA[
Thanks for your support <img class="smilies" src="https://forum.eggheads.org/images/smilies/icon_smile.gif" width="15" height="15" alt=":)" title="Smile"><p>Statistics: Posted by <a href="https://forum.eggheads.org/memberlist.php?mode=viewprofile&amp;u=9204">Elfriede</a> — Tue Mar 10, 2015 7:02 am</p><hr />
]]></content>
	</entry>
		<entry>
		<author><name><![CDATA[SpiKe^^]]></name></author>
		<updated>2015-03-12T17:00:58-04:00</updated>

		<published>2015-03-09T08:01:27-04:00</published>
		<id>https://forum.eggheads.org/viewtopic.php?p=103564#p103564</id>
		<link href="https://forum.eggheads.org/viewtopic.php?p=103564#p103564"/>
		<title type="html"><![CDATA[Compare text and output match in percentage ?]]></title>

		
		<content type="html" xml:base="https://forum.eggheads.org/viewtopic.php?p=103564#p103564"><![CDATA[
Explain what you are talking about Get_A_Fix...<p>Statistics: Posted by <a href="https://forum.eggheads.org/memberlist.php?mode=viewprofile&amp;u=7749">SpiKe^^</a> — Mon Mar 09, 2015 8:01 am</p><hr />
]]></content>
	</entry>
		<entry>
		<author><name><![CDATA[Get_A_Fix]]></name></author>
		<updated>2015-03-09T07:17:36-04:00</updated>

		<published>2015-03-09T07:17:36-04:00</published>
		<id>https://forum.eggheads.org/viewtopic.php?p=103563#p103563</id>
		<link href="https://forum.eggheads.org/viewtopic.php?p=103563#p103563"/>
		<title type="html"><![CDATA[Compare text and output match in percentage ?]]></title>

		
		<content type="html" xml:base="https://forum.eggheads.org/viewtopic.php?p=103563#p103563"><![CDATA[
<img alt=":thumbsup:" class="emoji smilies" draggable="false" src="//cdn.jsdelivr.net/gh/twitter/twemoji@latest/assets/svg/1f44d.svg"> willyw <img class="smilies" src="https://forum.eggheads.org/images/smilies/icon_smile.gif" width="15" height="15" alt=":)" title="Smile"><br><br>We need a rep button or something, I wonder if slennox can be bothered, or someone else possibly with access to template editing.<p>Statistics: Posted by <a href="https://forum.eggheads.org/memberlist.php?mode=viewprofile&amp;u=6204">Get_A_Fix</a> — Mon Mar 09, 2015 7:17 am</p><hr />
]]></content>
	</entry>
		<entry>
		<author><name><![CDATA[willyw]]></name></author>
		<updated>2015-03-08T09:15:23-04:00</updated>

		<published>2015-03-08T09:15:23-04:00</published>
		<id>https://forum.eggheads.org/viewtopic.php?p=103562#p103562</id>
		<link href="https://forum.eggheads.org/viewtopic.php?p=103562#p103562"/>
		<title type="html"><![CDATA[Re: Compare text and output match in percentage ?]]></title>

		
		<content type="html" xml:base="https://forum.eggheads.org/viewtopic.php?p=103562#p103562"><![CDATA[
There could be more than one way.   <img class="smilies" src="https://forum.eggheads.org/images/smilies/icon_smile.gif" width="15" height="15" alt=":)" title="Smile"><br><br>Go here :  <br><a href="http://www.tcl.tk/man/tcl8.5/TclCmd/contents.htm" class="postlink">http://www.tcl.tk/man/tcl8.5/TclCmd/contents.htm</a><br>and find:<br>string<br>and see the various ways to use that TCL command to manipulate strings.<br><br>I think you will be using:<br>string length<br>(along with a foreach loop, and you can find the command:<br>foreach<br>there too.  Use it to iterate through the string, char by char )<br>You'll want:<br>string equal<br>too.<br><br>Be sure to look up<br>expr<br>as that is how you do math.<br>Tip: <br>14/15<br>will return 0<br>While:<br>14/15.0<br>will return<br>0.9333333333333333<br><br>I hope this gets you started.  Actually, I hope you find a better way.   <img class="smilies" src="https://forum.eggheads.org/images/smilies/icon_smile.gif" width="15" height="15" alt=":)" title="Smile"><p>Statistics: Posted by <a href="https://forum.eggheads.org/memberlist.php?mode=viewprofile&amp;u=10420">willyw</a> — Sun Mar 08, 2015 9:15 am</p><hr />
]]></content>
	</entry>
		<entry>
		<author><name><![CDATA[Elfriede]]></name></author>
		<updated>2015-03-08T05:51:13-04:00</updated>

		<published>2015-03-08T05:51:13-04:00</published>
		<id>https://forum.eggheads.org/viewtopic.php?p=103561#p103561</id>
		<link href="https://forum.eggheads.org/viewtopic.php?p=103561#p103561"/>
		<title type="html"><![CDATA[Compare text and output match in percentage ?]]></title>

		
		<content type="html" xml:base="https://forum.eggheads.org/viewtopic.php?p=103561#p103561"><![CDATA[
Good morning everyone <img class="smilies" src="https://forum.eggheads.org/images/smilies/icon_smile.gif" width="15" height="15" alt=":)" title="Smile"><br><br>Im just asking myself, if theres a way to compare text like:<br><br>texta: I.am.just.here<br>textb: I.was.just.here<br><br>Bot should now compare those two textes and output the percentage of matching. Is there a way to do so ?<br><br>greets &amp; thanks<p>Statistics: Posted by <a href="https://forum.eggheads.org/memberlist.php?mode=viewprofile&amp;u=9204">Elfriede</a> — Sun Mar 08, 2015 5:51 am</p><hr />
]]></content>
	</entry>
	</feed>
