Mailing List Archive: 49091 messages
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 

[REBOL] Re: replacing urls in html using parse function...

From: al:bri:xtra at: 2-Sep-2002 22:22

Here's what Jean wrote (with a lot more spacing to make it easier to see what's going on): [ Rebol [] a: {assas href="http://www.ann.lu" dfdfdf src="http://bla.org" fdfdf} print "Parse 1" parse/all a [ any [ [ [ thru {href="} ] | [ thru {src="} ] ] copy text to {"} ( print text ) ] ] probe a print "Parse 2" parse/all a [ any [ [ [ thru {href="} ] | [ thru {src="} ] ] position1: to {"} position2: ( change/part position1 "neu" position2 print position2 ) ] ] probe a print "Parse 3" parse/all a [ any [ [ [ thru {href="} ] | [ thru {src="} ] ] position1: to {"} position2: ( change/part position1 "neu" position2 print position1 print position2 print "" ) ] ] probe a halt ] Jean wrote:
> It seems the parser is not at the right position after replacing the first
url. That's sort of right. Have a look at the console printout: Parse 1 http://www.ann.lu http://bla.org {assas href="http://www.ann.lu" dfdfdf src="http://bla.org" fdfdf} ; Here's the second one coming up: Parse 2 http://bla.org" fdfdf {assas href="neu" dfdfdf src="http://bla.org" fdfdf} Note that 'position2 seems to have printed out _too_ little information. At first glance, it should have printed: " dfdfdf src="http://bla.org" fdfdf Note that there 14 characters missing. Let's look at that 'change again: change/part position1 "neu" position2 The difference in length between "neu" and "http://www.ann.lu" is 14 characters. Clearly, when the 'change takes place, the 14 characters: p://www.ann.lu are removed from string value referred to by 'a. So that effectively moves the place that 'position2 is refering to by 14 characters further along. Parse 3 neu" dfdfdf src="http://bla.org" fdfdf dfdfdf srchttp://bla.org" fdfdf neu" fdfdf ** Script Error: Out of range or past end ** Where: do-boot ** Near: print position2 print "" Now when the value "http://bla.org" is replaced with "neu" in the third parse rule, the string is made another 11 characters shorter, so 'position2 falls off the end of the string, and you get the error message: ** Script Error: Out of range or past end To avoid this problem, readjust the position of 'position2. One can get the new position as a result of the 'change function:
>> help change
USAGE: CHANGE series value /part range /only /dup count DESCRIPTION: Changes a value in a series and returns the series after the change. So your parse action (inside the paren!) will look something like: position2: change/part position1 neu" position2 and the parse rule (outside the paren!) will look something like: ] position1: to {"} position2: ( ; blah blah blah... ) :position2 ; Here's where the position is reset. I hope that helps! Andrew Martin ICQ: 26227169 http://valley.150m.com/