Results 1 to 10 of 51

Thread: ब्लॉग कोशिश कर रहा है بلاگز کی ک*Trying Blogs

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #10
    Fuhrer, Vierte Reich DocAElstein's Avatar
    Join Date
    Aug 2014
    Posts
    9,316
    Rep Power
    10

    Recursion Wonk. To remove BB Code tag pairs incuding nested ones missus

    One problem noted in the previous codes was that the case of nested BB Code tag pairs, the codes did not work. At many forums such nesting of this form is permitted in BB codes.
    [color=Green]Comments [color=Red]Highlight[/color] Comments[/color]
    The returned result is
    Comments Highlight Comments

    None of the codes discussed so far will work for such a case.
    Should the Code tag type be dissimilar, such as_..
    [CENTER][B]Title[/B][/CENTER]
    _..then at least the Find Replace Wildcard Code will work for that by virtue of matching in the String first, for example, in the above example, _..
    __ CENTER __ __ __ __ CENTER __
    _.. then
    __ B __ __ __ __ B __
    However my Long string manipulation code will not even work for that.

    I have done a Long string manipulation code to be discussed in this and the following posts which , I think, work for all situations.

    Brief Descriptions is:
    The entire string is held in a variable, strBBCode. This is declared in a Subroutine. Something similar to that was done for the last code )

    change 1) Minor to last code:
    Unlike the previous codes, ( which sequentially removed BB Code code tags ( by replacing with "" )), the code tags are replaced by some arbitrary character ( This is simply chosen as any not likely to be used as text in the main initial string https://www.mrexcel.com/forum/excel-...ml#post4214083 )

    change 2) Minor
    This itself is not such a major change in the code format, but it facilitates the main change, 3) below. This change is that bulk of the code is within a Function, (Function LongWayOfDoingIt2ReCurseCyClops(__ ) . It is “Called” and needs to be given_..
    _.. the string, strBBCode, By Referral to that variable, rather than its By its Value contents
    and_..
    _.. a Value for the current character position, which initially will be the end ( or length in characters ) position of strBBCode.

    But those are not the main changed characteristic to the code.
    To recap: All my long string manipulation codes, including this Function, have the general characteristic that they loop backwards in the complete string until a ] is found. An attempt is made then to find a [/ . Once this is found an attempt is made to find a ] and then a [ .
    At this point a possible code pair requirement , looking backwards, of ] [/ ] [ is achieved
    ( Reversing that to the correct order shows that clearly more clearly,
    [ _ ] __ [/ _ ],
    or
    [codeText] AnyText [/CodeText] .
    Or
    [Color=red] AnyText [/color]
    _.. etc...)
    Having found a code tag pair, a check is made for its validity ( the same matching work, such as, codetext, color, for example ) , before the adjustments are made to the main string. After that , the code then moves on and steps further “back” in search of another pair_..
    _.. But just before it does that check, all codes, including this new Function, at the last found [ , do a check to see if in fact the next character after the last found [ is a / , indicating that in fact the assumed start tag, [codeText], was in fact another end tag, [/Codetext_b]. Action is then taken should that be the case.
    At this point the codes are very different. The action taken is different
    In the previous code, it was assumed that in such an event, the first found “end tag” was a rouge text looking like an end tag , but invalid, and not matched to a start code tag. The first found “end tag” was then ignored, and another search made for a matching start tag to the second found end tag.
    This results in an error situation for a valid nested BB Code tags situations.
    The new code works therefore much differently.

    Main change 3):
    In the new code, this point is around lines
    430 – 460, Rem If [/ Then Oh dear time for a wonk.
    Recursion is the name given for the general technique which at this point is used. It is whereby within a routine an instruction is given to run the routine.
    ... In other words the Routine “Calls itself”.
    So what is all that about? :
    ‘_- I will tell you, Matey boy:
    Any Function or Sub routine we write, is just a set of instructions. Like a piece of paper which is a Print out of the typed code instructions. It tells VBA what to do when it is run or called. I does not really physically go along or through the code lines: We just find that easy to visualise, or “look at “ when “stepping” through to debug a code. The code is just a set of instructions that VBA understands.
    So imagine you are VBA and you are given a set of instructions ( on a piece of paper ) to do a job, (for example you are given a written copy of the instructions to read and follow).
    You start doing the instructions.
    Now, say, before you are finished, you are given a set of identical instructions and are told to do that by the boss. What do you do, ( apart from swear at him ) ?
    The logical progression is, ( and that is what VBA does ), it stops what it is doing, so like freezes the initial running through the instructions , and starts again with the new ( duplicate as it happens in the case of “recursion”) set of instructions.
    Once finished it goes back and picks up the first job where you / it left off. Within reason, there is no limit to how often you can do that in VBA. If a code makes a Function or Sub routine keep “Calling” itself , then it “stacks” up in order in a “waiting list” the stopped set of instructions. As each set of instructions is completed, the last are continued at the point where they were interrupted and “frozen”. That point is code line 450
    It follows, ( although may not be immediate obvious as the same names are used) that all variables used in each run of the Function within the function are unique to that run of the function so do not influence the values of the similarly named variables in the other Function runs. An exception here is our main full string, strBBCode. This is because we have deliberately chosen to carry it over By such a way as to indicate it should be Referenced to within each Function. So in this case, changes made in any Function run to strBBCode will be Reflected in the main ( unique ) variable, strBBCode, which is initially “held” / declared in the initial calling code.
    So what is all that to do with the main change to this code version ??
    ‘_- I will tell you, Matey boy:
    Initially the Function is called the first time by a small Sub routine which passes to the Function two things
    _(i) the variable strBBCode. It is indicated via the use of ByRef in the Function signature ( first ) line, that specifically that variable is to be Referenced. ( You may effectively take a variable “into” the Function ( ByRef ) , or alternatively a Value. If an actual value is given, then giving it ByRef is just the same as By Value. If you pass a Variable ByValue the value at the time of the Call in the variable will be taken, and that variable will become local to the Function and so any changes to it made in the function will not be reflected in the variable “held” in the initial calling routine ........ )
    _...(ii) a number .......Value... is given which will be the start position to work back from when looping backwards through the characters in strBBCode. For this first Call, that will be the length of / or character count of , strBBCode.

    The Function code then transgresses as described and very similar to the previous code. It can go to completion in one run should no end code tag be encountered after an initial one is found and before a start tag is found.
    ____If a second end tag in sequence is found the "recursion wonk" kicks in.....
    450 _____ Call LongWayOfDoingIt2ReCurseCyClops(strBBCode, BcrdSEnd)
    So _ If this line is done, the current run freezes. A New Function begins. This is instructed to Refer to the unique main string strBBCode “held” in the original Calling routine. As Value for the current position in the backward stepping search is this time not the main string strBBCode length, but rather the position of the end ] in the currently found second sequential end code tag.
    The only thing that is certain at this point is that the new Function run will immediately find this second end cote tag as its first end code tag. This is probably inefficient as it repeats some steps done. ( A more complicated Function could be envisaged to carry across an Offset to be applied to “jump” this search and pass the end code tag )
    With no more nested code tag pairs, the second Function will complete the entire string, other than the possible code tag pair search “frozen” in the first Function.
    When the first function resumes, the initial code tag pair search is completed.

    There are many other scenarios including a further nested code tags section the second function finds and results in a third Function starting. That third Function would complete all but the two pairs searches “frozen” . At completion of the third Function, the second and then the first Functions with their respective code tag pair search would be completed.

    Another scenario would be a double nesting such that there are three sequential end code tags. Once again a second followed by a third Function would be started. Once again, the third Function would complete all but the two pairs searches “frozen”. At completion of the third Function, the second and then the first Functions with their respective code tag pair search would be completed.

    One can imaging many more scenarios

    One common factor is that all but the final function will be searching through the replaced replacement characters. This probably makes the code a bit inefficient

    Final usage of Function
    Initial code to Call the Function for the first time. ( The function may Call itself one or more times further )
    The Function will not run itself.
    It needs to be Called with the initial parameters passed, the main string, strBBCode, along initially with the length of that string as the start point from which to step back.
    A routine is needed for that.
    Additionally, once the initial Function run , ( which is set off by this routine ) is finished, we will have the original string, strBBCode , returned in a modified form with the replacement character in place of all found code tags. These need to be removed, which can be easily achieved by replacing them with zero length strings.
    An example code is given in the next post.

    A few minor last thoughts..
    This second solution for manipulating a long string text does not require stepping backwards. It was simply developed on from the last code. That code was easier to do backwards as in the code parts were deleted whilst stepping through the string: It was easier to deal with such a code in a backward loop, as things “behind me” are deleted. The problem with going forward is that “deleting in front of me” can require adjusting “where I am”, and the count to, or “final point of where I am going to”. – Think of it as you tripping up, or effecting where the end is, if you chop out the path in front of you: VBA can get messed up in Looping if you remove points in a progression that have not yet been considered.
    Another way of thinking about that:
    If I take bits out, then things shift back or down to fill the space. If I take things out in front of me, then by shifting back, things which I have not yet considered will change their position. This will likely mess up some order of progression.
    If I go backwards, and delete things behind me, then any order changes will only affect things which I have already considered. My original progression plan and order of things not yet considered is still valid.
    Last edited by DocAElstein; 02-27-2017 at 09:44 PM.
    ….If you are my competitor, I will try all I can to beat you. But if I do, I will not belittle you. I will Salute you, because without you, I am nothing.
    If you are my enemy, we will try to kick the fucking shit out of you…..
    Winston Churchill, 1939
    Save your Forum..._
    _...KILL A MODERATOR!!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •