Parsing Exercise
[1/4] from: edoconnor::gmail::com at: 1-Sep-2005 17:15
This string processing exercise is taken from the post:
http://discuss.joelonsoftware.com/default.asp?design.4.195596.22
Begin //////////////////////////
Given a text number of the follwing format:
AH-1-1
Or
AH-1-1G
Or
AH-1-G1
During data entry, we want the number code of the last part (token) to
increment by one.
So, if you pass the above values, you get
AH-1-1 --> AH-1-2 --> AH-1-3
AH-1-1G --> AH-1-2G --> AH-1-3G
AH-1-G1 --> AH-1-G2 --> AH-1-G3
////////////// End
I posted a REBOL version, but I'm not terribly gifted at writing parse
rules. I'd be curious to see how other REBOLers might handle it.
Of course, if you have an approach that is elegant and/or terse, reply
to the orginal poster.
Regards,
Ed
[2/4] from: SunandaDH::aol::com at: 4-Sep-2005 3:29
Ed:
> I posted a REBOL version, but I'm not terribly gifted at writing
> parse rules. I'd be curious to see how other REBOLers might handle it.
Just saw your message this morning -- or some reason it's on the archive but
didn't make it to my inbox.
It's never clear just how much validation should be involved in an exercise
like this. Should we be handling invalid cases where the last group does not
contain a number?
If the answer to that is no, then the code can be very simple - just need to
find and increment the last string of digits. In fact, it can be simpler than
below as my code is not a pre-parse solution. But it works, and that's
usually as far as I take things.
bump-id: func [id [string!]
/local
number
number-copy
digit
digits
][
digit: charset "0123456789"
digits: [some digit]
id: head reverse id
parse/all find id digit [copy number digits]
number-copy: 1 + to-integer head reverse copy number
replace id number head reverse form number-copy
id: head reverse id
return id
]
Sunanda.
[3/4] from: tom::conlin::gmail::com at: 5-Sep-2005 15:24
ids: ["AH-1-1" "AH-1-1G" "AH-1-G1"]
digit: charset "0123456789"
else: complement digit
rule: [any else here: copy n some digit there:
(change/part :here 1 + load n :there)
]
foreach id ids[parse find/last id "-" rule]
print ids
; ids are in a block of strings
; incremented part is after last hyphen
; incremented natural numbers never get shorter
On 9/1/05, Ed O'Connor <[edoconnor--gmail--com]> wrote:
> This string processing exercise is taken from the post:
> http://discuss.joelonsoftware.com/default.asp?design.4.195596.22
<<quoted lines omitted: 21>>
> To unsubscribe from the list, just send an email to
> lists at rebol.com <http://rebol.com> with unsubscribe as the subject.
--
.... nice weather eh
[4/4] from: edoconnor::gmail::com at: 6-Sep-2005 11:33
Thanks Tom, and thanks to all who contributed.
This exercise was very educational for me. Parse is one of those
really powerful features that requires a lot of practice.
Tutorial writers take note:
Parse is a great double-feature; it's one of the great practical
features of REBOL (text processing), as well as the gateway to
creating domain-specific languages (little languages, mini-declarative
languages, command and natural languages, etc.). If you want to show
practicality + advanced power, Parse should probably be the keystone.
Thanks again.
Ed
On 9/5/05, Tom Conlin wrote:
Notes
- Quoted lines have been omitted from some messages.
View the message alone to see the lines that have been omitted