This is the new home of the egghelp.org community forum.
All data has been migrated (including user logins/passwords) to a new phpBB version.


For more information, see this announcement post. Click the X in the top right-corner of this box to dismiss this message.

[SOLVED] Here we go again... (I got myself stuck)

Help for those learning Tcl or writing their own scripts.
Post Reply
d
dj-zath
Op
Posts: 134
Joined: Sat Nov 15, 2008 6:49 am
Contact:

[SOLVED] Here we go again... (I got myself stuck)

Post by dj-zath »

Hi Nml, Mindles, and Everyone!

First off, let me send out a big juicy "high-five" to Nml and Mindles for their awesomeness and help in the past.. I have learned a lot from these 2 guys.. they really know their stuff! (okay, not so much "juicy" but, how about a nice chilled 6-pack of their choosing? :))

And now...

Without getting into the psychcology (did I spell that right??) of the matter here... I am OCD.. and that is why the following occurred...

I recently was given some "suggestions" on new features for the website- including "auto podcast and archive creation" for my shows.

thats a GREAT idea!

So, I begin to "patch" some custom code I wrote.. and then *BAM*; I really BREAK it good.. so a rewrite is in order..

At this point, I decide to let a friend, who is a "expert programmer" have a peek inside.. The next thing I know, I'm hit with a barrage of all the things I'm doing wrong- without being told how to do them right.. of course, this person doesn't know TCL persey.. but he said "its much like C"... okay, fine... NOW the OCD kicks in.. (I do wonder if he did this as a form of torture?? HA HA HA HA)

One of the things he DID say, however, was that using "string matching" instead of integers was (in his words) "A BIG MISTAKE" and that I should use interger matching instead..

example: (what he suggests)

Code: Select all

if {($DetIA == "1")} {
      putlog "Det A is ON"
}
instead of: (what I used)

Code: Select all

if {($DetIA == "onair.gif")} {
      putlog "Det A is ON"
}
So, when I rewrote the code, I used integers instead of strings..

but now I hit the "big snarfu" where I have to convert these integers into their "onair.gif" and "offair.gif" notations respectively.. but 2 major problems came up right off the bat..

first one.. having to convert 20+ varables "on the fly" for EACH cycle that the output loop runs.. I see 20+ "if statements" here... that will run once per second; I fear that can be somewhat inefficient or CPU-intensive?

Code: Select all

if {$DetIA == "1"} {
      set DetIA "onair.gif"
} else {
      set DetIA "offair.gif"
}
simple, right? NO!

second problem.. the varables here are GLOBALS.. so they change EVERYWHERE.. including in the OTHER loops! Now, when the "parsers" loop runs once every 10 seconds and the "output" loop runs every second, for every 9 seconds, the "parsers" loops will see INVALID values!
(they see "onair/offair" instead of "1/0")

This, of course, BREAKS the parsers loop!

So, now one thinks "okay just rename all the globals- so that you can use "locals" in the output convertions.." right?

NO again!

Since these values are parsed INSIDE the website.. I HAVE to use them or fear rewriting everything in the website; OR.. add a new global for each local varable to "match" it... MESSY!

So, my first question to ask...

"is it better to match integers instead of strings as logical values? OR does it not really matter in this case... "

The EASIEST thing I can do, is to go and convert back the "output detect" varables to "onair.gif" and "offair"gif" as I've always used.. but, I also would like to do thiings "the right way", too..


a side note:
Sure, I would like to post the code here, but its 2200 lines! (not counting the "wrap-arounds"...) so I apologize if this is confusing to some.. also, posting in sections wouldn't work either since the loops are inter-twined and linked via the globals respectively.. means you'd have to see the whole thing anyways to follow along. :)

the second question:

"if using integers will make a considerable dfference here, and that its "the preferred way" (yes, I purposely put in two conditionals here) whats the best way to handle the output situation?"

I have more Q's coming in the near future.. and, perhaps, maybe I can commission Nml/Mindles to rewrite this thing? its probably a piece of [choclate] cake for them! YUM!

Let me thank everyone in advance, for any coments and suggestions on how I can get this thing up and running in as effeciently as possable :)

-DjZ-
:) :)
Last edited by dj-zath on Wed Jun 09, 2010 10:32 am, edited 1 time in total.
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

Lets see...

"string vs integer matching" - well, your friend not knowing tcl very well made his assumption based on his C-knowledge, where such a statement would make perfect sense. Using this kind of code in C would be a pointer comparison, rather than a string comparison.
In C, you'd actually do something along these lines for a string comparison:

Code: Select all

if (!strcmp(var, "on.gif")
{
  /* case if true */
} else
{
  /* case if false */
}
The tcl analogy would be this:

Code: Select all

if {![string compare $var "on.gif"]} {
  #case if true
} else {
  #case if false
}
Now for the technical part. Tcl variables do not have a specific type, but is cast to whatever type that makes sense for the current use. Internally, it will cache the last resulting cast'd type for efficiency, this however does not lead to any kind of preference (of type) in later use of the same variable. Thus, there really is no string or integer variables...
This leads to a very interesting result, both your comparison and the comparison your friend suggested would be equally efficient. Added that you then use the value of your variable to output text, your friends suggestion would actually be less efficient.

The only possible advantage of using a numeric ("integer"/"boolean") value/matching, is that the code might be easier to read for the human mind in some cases.
NML_375
d
dj-zath
Op
Posts: 134
Joined: Sat Nov 15, 2008 6:49 am
Contact:

Post by dj-zath »

hi nml buddy..

DING DING DING!!

Thats EXACTLY what I was thinking! the way I did it initally was the BETTER way because it required NO "conversion" at the end of the logic train as his method does.. (and, [his way] in fact, makes for a VERY MESSY ending nonetheless!).

I will now go and "put it all back" to the way I HAD it.. (of course, I did make some improvements in the parsers to further efficientize some of the string range varables)

your reply has made my OCD very happy :) you just made my day!

thank you sir! I appreciate your input more than you may realize :)

-DjZ-
:) :)
n
nml375
Revered One
Posts: 2860
Joined: Fri Aug 04, 2006 2:09 pm

Post by nml375 »

For the future, if you are concerned with the efficiency of different approaches, try the "time" command. It'll run the code you provide a given number of times, measuring the average execution time.

Code: Select all

set var1 1
set var2 "on.gif"

proc proc1 {} {
  if {($::var1 == "1")} {
    return true
  } else {
    return false
  }
}
proc proc2 {} {
  if {($::var2 == "on.gif")} {
    return true
  } else {
    return false
  }
}

puts stdout "Testing proc1, 10,000 executions: " nonewline
puts stdout [time proc1 10000]
puts stdout "Testing proc2, 10,000 executions: " nonewline
puts stdout [time proc2 10000]
In this case, you'll see how little difference there is between the two approaches. On my 600MHz Via Eden system, the difference was 3-4 microseconds.. You'd have to run the code 250,000 times to notice 1 second of performance-loss...
NML_375
d
dj-zath
Op
Posts: 134
Joined: Sat Nov 15, 2008 6:49 am
Contact:

Post by dj-zath »

thanks for the code and thought.. but, I was more concerned about "doing it right" as opposed to efficiency in that; "it wouldn't take that long", however , more that what I had to do, in the end, to "turn it all back into strings" was just too much to bare.. so I went ahead and put all the strings back in!

sealed it DONE!

the end result was exactly what I have done all-along! I'm not saying I was "doing it the right way" and I have my doubts (which is why I attempted to change it in the first place thats OCD for ya) so I tried a different approach based onsoemone else objectives and suggestions..

didn't work! :)

-DjZ-
:) :)
Post Reply