Unobvious (?) technique
Jeff,
J> for i = 1 to length(s)
J> if s[i] = keyval then
J> s[i] = newval
J> end if
J> end for
J>
J> There's another way which seems to be somewhat faster:
J>
J> s = (newval * (s = keyval)) + (s * (s != keyval))
J>
J> I know _why_ it works, but when I tried to write out an
J> explanation, it ran to several pages. If there are any
J> mathematicians here, they're welcome to try to explain it in
J> language that we can all understand...
Interesting, yes? Let me give my interpretation.
Only (s = keyval) or (s != keyval) may be true at any given time.
True is a one, not true is a zero. So it will equate to either
s = (newval * 1) + (s * 0) which is: s = newval + 0 or
s = (newval * 0) + (s * 1) which is: s = 0 + s
since 0 times anything is zero and 1 times anything is itself.
Now what you have here is a Boolean logic equation.
To use it more easily, you can equate '*' to
mean IF and '+' to mean OR. There is more to it, but just reread
and it says: s = newval IF s = keyval, OR s = s IF s != keyval.
---
* SRP 2.00 #2663 *
|
Not Categorized, Please Help
|
|