ID:155709
 
I know there must be a smarter syntax here. It seems like somehow I should be able to both assign the variable and check whether the item on the right-hand side of the assignment exists, at the same time. Can someone explain how I could shorten:

proc/type2text(str)
var/i
while(findtext(str,"/"))
i = findtext(str,"/")
str = copytext(str,i++)


I tried this, but it's not quite right:

proc/type2text(str)
var/i
while(i = findtext(str,"/"))
str = copytext(str,i++)


Interestingly enough, I thought this should be syntactically valid, but it's not (the errors I get are kind of silly):

proc/type2text(str)
var/i
i = while(findtext(str,"/")) {
str = copytext(str,i++) }



Cheers

-- John M.
Your first attempted fix wouldn't even initialize the loop since i is null.

Your second attempted fix is silly (not the errors being silly). In no language can you make a variable equal to a loop like that.

Your main attempt will yield an infinite loop should there be ANY form of text (whether it be a space, symbol, etc) after the forward-slash.

Your basic rule for using a loop is that any conditions being tested by the loop must change at some point during the loop or else an infinite loop will occur.
while(1) //infinite loop

var/x=1
while(x) //infinite loop

var/x=1
while(x) //finite loop since x changes
x --

var/x=1
while(x) //infinite loop because x will always be a true value
x ++

var/x=1
while(x < 10) //finite loop; x will eventually hit 10
x ++

When using loops, you have to be careful and make sure that the loop WILL end eventually.
In response to Spunky_Girl
Ah yes, I did make a mistake.

It should be ++i. This shouldn't cause an infinite loop, because it should necessarily remove the "/" at some point.

Did you have a comment that actually applies to my OP, though? It would be cool if there was a way to reduce the code here. It doesn't seem optimized to me. Updated (but possibly inefficient code looking for help) here:

type2text(str)
var/i
while(findtext(str,"/"))
i = findtext(str,"/")
str = copytext(str,++i)
return str


Cheers

-- John M.
In response to BigJMoney
Could you please explain what the proc's purpose is? I have a guess but it'd be easier if I knew for sure.
In response to Murrawhip
The purpose is to take the text string version of a path, like: "mob/char/player" and boil it down to the string of its deepest child type. So, in this case: "player".

The specific bit I am sure can be optimized though is this, which just looks inefficient to me:

        while(findtext(str,"/"))
i = findtext(str,"/")


It seems like I should not be running findtext() two separate occasions to retrieve the same value.

-- John M.

In response to BigJMoney
This seems to accomplish what you're after, and works for the test cases I came up with.

proc/findchild(path)
var/i
for(i=length(path);i>1;--i)
if(copytext(path,i-1,i)=="/")
break
return copytext(path,i)


Edit: And doesn't calculate anything unnecessarily, which was your goal, I think.
In response to Murrawhip
Ah neat, you did something even better than I was thinking. I guess this points out that findtext isn't the best option when you want to find a symbol that's likely to be near the end of the string.

Cool!