Home   Archive   Permalink



Parse issue with change/part

I have the following parse issue. In the first sample text below, the parse will hit the two command blocks as it finds the parts in the text.
    
Give the below a try as is (Rebol 2).
    
sample-text: {<a href="javascript:gotoURL('displayContent.aspx?contentID=9&language=english#Deferred-member');">deferred member</a>}
    
remove-anchors: func [sample-text][
    parse sample-text[
        some [
            to {<a href="javascript:gotoURL('displayContent.aspx?contentID=9}
            begin:
            thru {);">}
            ending:    
            (print "Command 1 executed" )
            to "<"
            begin:
            thru ">"
            ending:
            (print "Command 2 executed" )
        ]
    ]
    return the-buffer
]
    
__________________
Result:
>> remove-glossary-anchors sample-text
Command 1 executed
Command 2 executed
__________________
    
However, if insert the change/part portion of the command that is expected to remove the text it finds, after the first change/part executes it appears the parse stops.
    
    
sample-text: {<a href="javascript:gotoURL('displayContent.aspx?contentID=9&language=english#Deferred-member');">deferred member</a>}
    
remove-anchors: func [sample-text][
    parse sample-text[
        some [
            to {<a href="javascript:gotoURL('displayContent.aspx?contentID=9}
            begin:
            thru {);">}
            ending:    
            (print "Command 1 executed" change/part begin "" ending)
            to "<"
            begin:
            thru ">"
            ending:
            (print "Command 2 executed" change/part begin "" ending)
        ]
    ]
    return the-buffer
]
    
__________________
Result:
>> remove-glossary-anchors sample-text
Command 1 executed
== "deferred member</a>"
__________________
    
    
Since I have multiple different types of links in the texts I'm trying to remove these pieces of HTML from, and multiple occurences in the same text, I figured PARSE was the right solution.
    
Can anyone see what I am doing wrong?


posted by:   Brock       29-Aug-2014/16:23:04-7:00



Hi Brock,
    
I think stackoverflow.com is the best place for Q&A, and a forum is more a place for talking about things that are not Q&A but rather discussion. There are a lot of advantages to the stackoverflow model... in terms of UI, editing, curation. You have a couple of corrections needed in your example like "remove-anchors" instead of "remove-glossary-anchors" and "the-buffer" instead of "sample-text". StackOverflow has a very nice model for this, and formatting, and how trust is distributed to let people do the fixing to make for good institutional knowledge. And of course, le chat:
    
http://chat.stackoverflow.com/rooms/291/rebol-and-red
    
But as for answering the question, the thing to understand about a series position is it just a series and an offset. Consider this:
    
>> foo: [a b c]
== [a b c]
    
>> bar: next foo
== [b c]
    
>> take foo
== a
    
>> probe bar
[c]
    
== [c]
    
When you are changing the series out from underneath PARSE, the same behavior applies. The "parse position" is just a series plus an integer--so if you are change the input series in PAREN! code then the parse will be proceeding with that series plus integer just as it was. You must fix up the position if the integer representing the position is no longer what you want.
    
I suspect what you wanted was:
    
         some [
             to {<a href="javascript:gotoURL('displayContent.aspx?contentID=9}
             begin:
             thru {);">}
             ending:
             (print "Command 1 executed" change/part begin "" ending)
             :begin ;-- IMPORTANT! Adjust parse position after change
             to "<"
             begin:
             thru ">"
             ending:
             (print "Command 2 executed" change/part begin "" ending)
         ]
    
But again, please consider StackOverflow for Q&A that is not discussion-oriented.

posted by:   Hostile Fork       29-Aug-2014/17:06:26-7:00



(...you would have to do this after command 2 also, obviously!)

posted by:   Hostile Fork       30-Aug-2014/0:12:30-7:00



Thanks Hostile Fork. It was placed into StackOverflow after thinking this wasn't the place. I had pruned some stuff and changed names to try to get a lighter weight version of what I am after, thanks for pointing out the issues with what I posted.

posted by:   Brock       2-Sep-2014/12:10:54-7:00