World: r3wp
[I'm new] Ask any question, and a helpful person will try to answer.
older newer | first last |
mhinson 14-May-2009 [2203] | ah, do I need a copy result to perhaps? |
Ladislav 14-May-2009 [2204x3] | if {copy result to ...} is what you want then I understand why you are having trouble with the END |
not-comma: complement charset "," copy result any [not-comma | skip] | |
sorry, just copy result not-comma | |
PeterWood 14-May-2009 [2207] | I think the advice to break the problem down and use more than one parse statement is good advice. Let's see how we can do that. I'll go through how I would approach the problem step by step. Sorry that I'll be squashing things on to one line but AltME paste doesn't work well on Mac. |
mhinson 14-May-2009 [2208] | :-) |
PeterWood 14-May-2009 [2209] | I usually try to take a few small steps rather than try to get the answer straight away. The test data: >> inp: {random 2/2,2/4-6,2/33-37} == "random 2/2,2/4-6,2/33-37" |
mhinson 14-May-2009 [2210] | random is sometimes a word & sometimes a number between 1 & 1000 |
PeterWood 14-May-2009 [2211x2] | We'll need to look for digits so : > digit: charset [#"0" - #"9"] == make bitset! #{ 000000000000FF03000000000000000000000000000000000000000000000000 } |
Does random ever have the sequence 2/number in it? | |
mhinson 14-May-2009 [2213x2] | no |
specific words (not many of them) on numeric alone | |
PeterWood 14-May-2009 [2215] | So what we need to look for are the patterns 2/number and 2/number-number. |
mhinson 14-May-2009 [2216x3] | on=or |
or 3/number...etc or 4/ or 12/ | |
but not on the same occasion | |
PeterWood 14-May-2009 [2219x2] | Let's start with 2/ and see how we get on >> parse/all inp [ any [copy range "2/" some digit (print range) | skip ]] 2/ 2/ 2/ |
So I've found all the sequences of 2/ but didn't capture the following digits. Can you see my mistake? | |
mhinson 14-May-2009 [2221] | I see why it has done that but I dont know how to extend the copy to the comma except with to #"," |
PeterWood 14-May-2009 [2222x2] | Let's change the parse rule so that I capture more than just the 2/: >> parse/all inp [ any [copy range ["2/" some digit] (print range) | skip ]] 2/2 2/4 2/33 == true |
You don't need to worry about the commas. skip will take care of them. | |
mhinson 14-May-2009 [2224] | so copy range has captured everything up to the point it no longer matches the expression in the [] a bit like a regular expression match. |
PeterWood 14-May-2009 [2225x2] | So now I'm capturing the first number after the 2/ but not the range. So let's add a little more to the parse rule to capture that: >> parse/all inp [ any [copy range ["2/" some [digit | "-"]] (print range) | skip ]] 2/2 2/4-6 2/33-37 == true |
Yes copy captures everything that it matches. | |
mhinson 14-May-2009 [2227] | a bit like copy thru? |
PeterWood 14-May-2009 [2228x2] | You only need to use thru when you can't specify exactly what you want to copy. |
I think of it this way: copy variable pattern-to-copy copy variable pattern-to-start-copying-from thru pattern-to-stop-copying-at | |
mhinson 14-May-2009 [2230] | So essential in cases where the middle bit is truly unpredictable |
PeterWood 14-May-2009 [2231x2] | Yes |
Now we can change the parse rule to work with 3/. 4 and /12: > parse/all inp [ any [copy range [["2/" | "3/" | "4/" | "12/"] some digit] (print range) | skip ]] 2/2 2/4 2/33 == true | |
mhinson 14-May-2009 [2233] | the 2/ part can be any number from 1 to 13 |
PeterWood 14-May-2009 [2234] | Do you want to capture all of them? |
mhinson 14-May-2009 [2235] | absolutely |
Ladislav 14-May-2009 [2236] | one-to-thirteen: ["1" digit | non-zero-digit] |
PeterWood 14-May-2009 [2237] | Then we can come up with a pattern for them: digit opt digit "/" |
mhinson 14-May-2009 [2238] | could we use 1 2 digit #"/" ? |
Ladislav 14-May-2009 [2239x2] | at-most-three: [#"0" - #"3"] non-zero-digit: [#"1" - #"9"] |
one-to-thirteen: ["1" at-most-three | non-zero-digit] | |
PeterWood 14-May-2009 [2241] | I'm presuming that the only possible values are 1 to 13. |
mhinson 14-May-2009 [2242] | they are, but there will never be a 0/ or 22/ that we want to avoid in this case |
PeterWood 14-May-2009 [2243x3] | Ladislav's will select values 1 to 13 from any numbers and so is more correct. |
This is the amended rule: >> parse/all inp [ any [copy range [[digit opt digit "/"] some digit] (print range) | skip ]] 2/2 2/4 2/33 == true | |
Oops I've lost the range again: >> parse/all inp [ any [copy range [[digit opt digit "/"] [some digit "-" some digit]] (print range) | skip ]] 2/4-6 2/33-37 == true | |
Steeve 14-May-2009 [2246] | To be more lecturable, perhaps you could deal with named sub-rules |
Ladislav 14-May-2009 [2247x2] | yes, we should describe the structure using named parts like: record: [random-part whitespace repeated-part any ["," repeated-part]] |
mhinson, does the structure I described correspond to what you need? | |
PeterWood 14-May-2009 [2249x3] | That's a good idea but I was being lazy partly because it's such a pain to copy and paste in AltME (it strips out all line endings). |
Here's the big rule now fixed : >> parse/all inp [ any [copy range [[digit opt digit "/"] [some digit opt ["-" some digit]]] (print range) | skip ]] 2/2 2/4-6 2/33-37 == true | |
Ladislav, that's my understanding | |
Steeve 14-May-2009 [2252] | rewrote Peter's with named sub-rules prefix: [digit opt digit] sufix: [some digit opt ["-" some digit]] target: [prefix #"/" sufix] parse/all inp [any [copy range target (print range) | skip]] |
older newer | first last |