Using parse to make intelligent substitutions.
[1/8] from: mat::plothatching::com at: 28-Apr-2003 16:54
Hello Mat,
MB> Here's my stab;
And of course a few minutes later (and about half as long as it takes
mails to get posted on the list) I work out how to use 'some digits'.
:)
So here's my revised go, this time I'll make a function;
replaceblogstr: func [
inputtext
/local digits blogsfound foundblog
][
digits: charset "0123456789"
blogsfound: copy []
parse inputtext [any [thru "blog" copy foundblog some digits (append blogsfound to-integer
trim foundblog)]]
foreach foundblog blogsfound [
replace InputText rejoin["blog "foundblog] rejoin[{<A HREF="http://www.blah.com/blog.php?blog=}foundblog{">blog
}foundblog{</A>}]
]
InputText
]
Much less sucky! Is that as good as it can get?
Regards,
Mat Bettinson - +44-(0)20-83401514.
[2/8] from: mat:plothatching at: 28-Apr-2003 16:27
Hello,
Interesting thing I had to do today. Take a block of test and if
there's any occurrence of "blog" followed by a number, then this into a
URL. I thought it about time that I actually got off my rear and
worked out Parse to a little more advanced level than my usual going
around the houses.
Here's my stab;
<--cut-->
InputText: "This is blog 42 and then some stuff followed by blog 54 right?"
blogsfound: copy []
parse test [any [thru "blog " copy blognumstring to " "
(if not error? try [blognum: to-integer blognumstring] [
append blogsfound blognum
])
]
]
foreach foundblog blogsfound [
replace InputText rejoin["blog "foundblog] rejoin[{<A HREF="http://www.blah.com/blog.php?blog=}foundblog{">blog
}foundblog{</A>}]
]
>> print InputText
This is <A HREF="http://www.blah.com/blog.php?blog=42">blog 42</A> and then some stuff
followed by <A HREF="http:
//www.blah.com/blog.php?blog=54">blog 54</A> right?
<--cut-->
It works but it's ugly. Another way, I guess, would be to build a
complete new string with the substitutions from within the parse
statement.
However I'd love to see someone's proper elegant Rebolesque way of
doing it that will end up with me slapping my forehead going "Duh!"
again. After gazing at lots of Parse documents I couldn't work out how
I could use 'some digits' type stuff to parse on and then end up with
the digits being passed to the rebol expression doing all the
replacing and stuff.
Thanks guys. I love Rebol, I just wish I was better at it :)
Regards,
Mat Bettinson - +44-(0)20-83401514.
[3/8] from: mat:plothatching at: 28-Apr-2003 17:20
Hello Mat,
MB> parse inputtext [any [thru "blog" copy foundblog some digits (append blogsfound
to-integer trim foundblog)]]
Bah only this doesn't work because it only does the first occurrence of
blog
. OK I'm stuck :)
Regards,
Mat Bettinson - +44-(0)20-83401514.
[4/8] from: chris:ross-gill at: 28-Apr-2003 13:05
Hi Mat,
> Bah only this doesn't work because it only does the first occurrence of
> "blog". OK I'm stuck :)
Just so happens I've been wading through parse rules a fair bit the last few
days, though that may also mean my solution is slightly overcooked.
- Chris
--
blog-ctx: context [
digits: charset "0123456789"
mark: foundblog: none
blog-rule: [
any [
to "blog" mark:
"blog" copy foundblog some digits
(
replace mark join "blog" foundblog rejoin [
mold build-tag compose [
A HREF (
join http://www.blah.com/blog.php?blog trim foundblog
)
] "blog " foundblog </A>
]
)
thru </A>
]
]
set 'replaceblogstr func [inputtext][
parse inputtext blog-rule
inputtext
]
]
[5/8] from: mat:plothatching at: 28-Apr-2003 19:32
Hello Christopher,
Thanks for your reply!
CRG> Just so happens I've been wading through parse rules a fair bit the last few
CRG> days, though that may also mean my solution is slightly overcooked.
That's quite interesting, it's definitely what I was looking for the
way you've got mark: in there and searching and replacing on that.
I found out why my last one didn't work past the first one, obvious
really. The subrule needs to return positive for the any to continue.
So the solution was to change some digits to any digits. This required
the addition of a conditional on the temporary variable to check to
see if there's anything in 'foundblog'. Here's my production code;
replaceblogstr: func [
inputtext
/local digits blogsfound foundblog
][
digits: charset "0123456789"
blogsfound: copy []
parse inputtext [any [thru "blog " copy foundblog any digits
(
if foundblog [append blogsfound to-integer foundblog]
)
]
]
foreach foundblog blogsfound [
replace InputText rejoin["blog "foundblog] rejoin[{<A HREF="http://www.blah.com/blog.php?file=blog}foundblog{.txt">blog
}foundblog{</A>}]
]
InputText
]
This works and isn't so kludgy that I insist on rewriting it just on
principal. Thanks for your example, that technique is going to help when
I do other things like this in the future.
Certainly it's going to let me write complex parsing stuff faster and
smaller in the future. I've *really* jumped through some hoops just to
get things to work in the past.
I guess I should probably use build-tag as well but I've been doing it
with strings by hand for so long now it'll be a hard habit to kick. :)
P.S. Has anyone written a function to get results from Google's SOAP
interface? I've got loads of stuff based on parsing HTML but I'd like
to switch to the SOAP interface as it's obviously more efficient.
Regards,
Mat Bettinson - +44-(0)20-83401514.
[6/8] from: gchiu:compkarori at: 29-Apr-2003 13:30
On Mon, 28 Apr 2003 19:32:39 +0100
Mat Bettinson <[mat--plothatching--com]> wrote:
>P.S. Has anyone written a function to get results from
>Google's SOAP
>interface? I've got loads of stuff based on parsing HTML
>but I'd like
>to switch to the SOAP interface as it's obviously more
>efficient.
Use guest for userid and password if prompted
http://203.79.110.37/rebolml/rep-getmsg?m_no=21024
--
Graham Chiu
http://www.compkarori.com/vanilla/
[7/8] from: tomc:darkwing:uoregon at: 28-Apr-2003 23:11
heres one
base: {http://www.bla.com/blog.php?blog=}
offset: 7 + lenght? base
parse {blogblogblog this is blog 73 and this is blog to skip then blog 1
and done}
[
any[thru " blog " mark:
[
[copy blog integer!
(insert :mark rejoin[{<a href="} base blog {">}]
insert find skip :mark offset " " "</a>"
)]
|[skip]
]
]
to end
]
On Mon, 28 Apr 2003, Mat Bettinson wrote:
[8/8] from: tomc:darkwing:uoregon at: 28-Apr-2003 23:48
please excuse the typos
lenght? should be length?
and
parse {blogbl ... } should be more like
parse s: {blogbl ...}[
if the results are to be caught
too much rearranging w/o testing
On Mon, 28 Apr 2003, Tom Conlin wrote: