Sorry I haven't commented recently, I've been pulled away for a few days.
Should be back on top of things once I get my head wrapped around this again.
K, I think thats all the pieces you need so-far. Read what i gave you several times, i was very literal in my wording.
BTW, How are we doing?
Phew, there's a lot of info there. I do understand I think regarding the use of the variables now. This has been a good description and learning experience.
I have a couple questions regarding what Lee mentioned:
Just to elaborate on this slightly, let us say we have two (or more) functions, and we wish for variables to be accessible in both, eg;
< ... > we define (and declare) the variable 'var1' in function foo and reference it in function bar. Notice that the variable is not localised in bar, as this would render it's value null upon calling bar from foo.
This is in essence, what I have going on in my code above.
This method of referencing variables between functions is called 'lexical scoping' if I remember from Se7en correctly...
So you can effectively view subfunctions as elegant 'space-savers', instead of duplicating code over and over.
IMO (for what its worth), I prefer to pass arguments where I can, and avoid referencing variables across multiple functions unless I absolutely have to - I feel this makes the intention clearer and easier to understand. Not to mention it makes the subfunction useful as a stand-alone function (or library function if you will).
Lee
Now I have read from other posts in the past that cutting and pasting code to put together a new routine is fine, as long as it all works out in the end. I've found that errors can be horrendous by doing this unless you keep very careful track of everything. But if you are cutting and pasting code from other routines, you will have a lot of these "Lexical scoping" issues. By Lee's opinion, you don't like to do this, correct ?? You'd rather have one definition and the code inside to run from that definition. Would you consider this a good practice though for it's purposes ?? I mean, I like 'lexical scoping' for several reasons; it can be used in other definitions without rewriting the code. It makes the routine as a whole, easier to follow and troubleshoot as you are working with sections rather than the whole. But there are the drawbacks, like as was mentioned, variables are referenced across multiple functions.
There was a lot of information given here, and there are still some things I am confused over, but I can't put my finger on them yet to post a question. Or in other words, I haven't yet thought through it from beginning to end, to post a question that hasn't already been answered.
Now looking back at my code, I can see that the piece here:
(if (/= OldText "String") ; if NOT replacing entire string
(SelectAllStrings) ; Then select ALL text
(SelectEachString) ; Else select strings to be replaced
) ; _if
referencing these:
(defun SelectEachString (/ )
(while (= AllText nil)
(prompt "\nSelect text to be replaced: ")
(setq AllText (ssget '((0 . "TEXT,MTEXT")))) ; ,MULTILEADER
)
(ProcessText)
)
;
(defun SelectAllStrings (/ )
(prompt "\nSelect text to be replaced (Press 'Enter' for all text in drawing): ")
(setq AllText (ssget '((0 . "TEXT,MTEXT"))))
(if (or (= AllText nil) (= AllText ""))
(setq AllText (ssget "_X" '((0 . "TEXT,MTEXT")))); ,MULTILEADER
)
(ProcessText)
)
Could be all-inclusive to the main segment of code and probably save a few lines. Although I don't see why it would matter (for this small piece of code) as there is no variables in either that are being referenced somewhere else. From the main segment to these definitions, it is a simple open & close process.
The third section is different however, as it does go back and forth from the main section.
Can I assume you are seeing something in my code that is prompting you to go in this direction, that is, toward this instruction of variables ??
As CAB mentioned:
Hangman,
FYI, First thing you do when debugging is to disable the local error handler.
I ran the routine with out error but it did not replace the text.
Which is what is happening to me as well. What I am not understanding is I can edit the code with something simple like adding a comma to a comment line and saving the code and reloading it, and it works fine. It is just the initial time it is loaded it will not run, it errors.
I know the error occurs in the main segment of code and not in any of the 'lexical scoping' sections (or 'sub-functions' perhaps?). But there's another drawback to lexical scoping I guess, you can have a variable referenced in one of the sub-functions and it could cause an error in the main section. But that doesn't make sense here (again, trying to think this through) as, by stepping through the code, it would have to get toward the end of the main section before transfering to the third definition (processtext) to find a variable NOT being initialized to cause an error. And so far, it is not doing this. My code is erroring before getting to the third definition, ... it is erroring before getting to the second definitions.
I am thinking if I rewrite this, I can probably make it work as I want it to, eliminating the error as I go. But I want to find this error so I can avoid making the same mistake again later with some other similar piece of code I may put together.
Thank you for your continued help.