Brock
Brock

Reputation: 33

Parse issue when using Change command (Rebol2)

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 (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 sample-text
]

Result:

remove-anchors sample-text
Command 1 executed
Command 2 executed

However, if I insert the change/part portion of the command, which is expected to remove the text it finds, the first change/part executes but it appears the second portion of the parse command stops as the second execution block doesn't trigger.


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)        ;<<----- change
            to "<"
            begin:
            thru ">"
            ending:
            (print "Command 2 executed" change/part begin "" ending)        ;<<----- change
        ]
    ]
    return sample-text
]

Result:

remove-anchors sample-text
Command 1 executed
== "deferred member</a>"

Note the second command didn't seem to execute both by the Print not executing and the parse not completing.

Since I have multiple different types of links in the texts I'm trying to remove these pieces of HTML from, and multiple occurrences in the same text, I figured PARSE was the right solution.

Can anyone see what I am doing wrong?

Upvotes: 1

Views: 54

Answers (1)

sqlab
sqlab

Reputation: 6436

Your function should work if you use this

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)       
        :begin                                                  ; note this
        to "<"
        begin:
        thru ">"
        ending:
        (print "Command 2 executed" change/part begin "" ending)       
    ]
]
return sample-text

]

Explanation:
The internal parse pointer is at an internal numeric index of 95 after {);">}. After the change command the index is still at 95, but the sample-text is now much shorter and your parse pointer after your second search text "to "<", probably already after the end. You can see that if you use this line

   (print "Command 1 executed" change/part begin "" ending  print ending)        ;<<----- change

in your function, giving you following error

** Script Error: Out of range or past end
** Where: remove-anchors
** Near: print ending

So you have to set back your parse index / pointer to the beginning of the point, where you changed/deleted your text. This you get with :begin after your alteration.

Best advice is to set back / initialize your internal parse pointer anew, if you modified your parse input: After deletion you should go back to the start of your deletion and after insertion / modification you should go first to the start and then to the end of the new item.

Upvotes: 1

Related Questions