World: r3wp
[I'm new] Ask any question, and a helpful person will try to answer.
older newer | first last |
PeterWood 23-Jun-2009 [3070x2] | Some and Any are forms of repetition, this shows the difference: >> parse "aaa1000" [some "a" to end] == true >> parse "aaa1000" [any "a" to end] == true >> parse "bbb1000" [any "a" to end] == true >> parse "bbb1000" [some "a" to end] == false |
I apologise if you are already happy with these basic concepts, in which case I hope you don't mind the refresher. | |
sqlab 23-Jun-2009 [3072x3] | Maybe these are some variations of what you are looking for parse/all "fd doixx s x x x oie x } " [some [copy d "x" (print d) | skip]] parse/all "fd doixx s x x x oie x } " [some [copy d 1 2 "x" (print d) | skip]] parse/all "fd doixx s x x x oie x } " [some [copy d 2 "x" (print d) | skip]] parse/all "fd doixx s x x x oie x } " [some [copy d "xx" (print d) | skip]] parse/all "fd doixx s x x x oie x } " [some [[copy d "x" copy e "x" (print [e d]) ] | skip]] parse/all "fd doixx s x x x oie x } " [some [ (g: copy "" ) 2 [copy d "x" (append g d) ] (print g ) | skip]] |
or you are looking for the pairs parse/all "fd doixx s x x x oie x } " [ some [ [ (g: copy "" ) 2 [ copy d "x" (append g d ) any notx | skip ] (if not empty? g [print g]) ] ] ] | |
I forgot notx notx: complement charset "x" parse/all "fd doixx s x x x oie x } " [ some [ (g: copy "" ) 2 [ copy d "x" (append g d ) any notx | skip ] (if not empty? g [print g]) ] ] | |
mhinson 23-Jun-2009 [3075x2] | Thanks PeterWood. I like to think I am ok with the most basic concepts, so now I am trying to learn things that will help me some my real life probelms in a better way. I use parse pretty much every day & always have a rebol console up on my work PC, but ANY SOME & OPT & | I do not understand in context. I understand them in abstract terms, but not how to apply them in conjuction with [] . I do understand your examples of some & any (these examples are usefull to me). skipping an un-known number of chars to get to the next match is the bit I find hard to understand how to construct, paticularly if it needs to be done in the context of a previous match. |
sqlab, I dont know about this syntax at all. I dont think I understand what is happening here. copy to "x" & copy thru "x" I understand, but copy "x" I didn't expect to see. | |
BrianH 23-Jun-2009 [3077] | In the parens you use the COPY function, not the PARSE copy operation. Is that what you meant? |
mhinson 23-Jun-2009 [3078x3] | The compliment syntax & the to 1 3 digit where digit is a charset seems to be "unreliable" as far as I can understand. |
this is what I dont expect. parse/all "fd doixx s x x x oie x } " [some [copy d "x" (print d) | skip]] | |
I dont think I have ever seen the PARSE copy operation documented. I will have a hunt for it. | |
Maxim 23-Jun-2009 [3081] | have you ever read the parse documentation in the old RT publisehd rebol 2.3 pdf ? its a good reference... there are only minor changes from that version up to the latest... I don't think any of the examples would fail in the current parse. |
mhinson 23-Jun-2009 [3082] | chpter 15 or the Rebol Core Manual http://www.rebol.com/docs/core23/rebolcore-15.html may have a use of this syntax in a complicated example, but no description of what is happening exactly. |
Maxim 23-Jun-2009 [3083] | yep, that's the online version of it. |
BrianH 23-Jun-2009 [3084] | OK, here's what happens: The next recognized pattern is COPY/part'ed and assigned to the variable. If the length of the matched pattern is 0, #[none] is assigned to the variable. |
mhinson 23-Jun-2009 [3085x2] | Yes, I have read it a lot, but it seems more of a reference for people who already know, rather then an explanation of Parse operations. |
Thanks BrianH, I was sort of guessing it must be like a variation of copy thru "x" that does not skip like thru... I think I get that now. Thanks. | |
BrianH 23-Jun-2009 [3087x2] | Note that the assignement to the variable happens *after* the pattern is recognized, so any code inside the pattern that references the value of the variable will get the old value. Like this: >> x: "old" == "old" >> parse "new" [copy x ["new" (print x)] (print x)] old new == true |
The same goes for the set operation of block parsing. | |
mhinson 23-Jun-2009 [3089] | That is pretty important! I had not realised that before & this copuld account for some of the unpredictable behaviour I get.. I thought the patern was complete at your first print statement. These [] have lots of subtle influence. |
BrianH 23-Jun-2009 [3090] | [ and ] are a grouping construct. |
mhinson 23-Jun-2009 [3091] | This is my nemisis. I can't understand how this prints XXXX then XX , not XX three times. It seems to have a will of its own. parse/all { X X XX X X} [some[[copy x "X" (prin x) [copy y "X" (print y) | skip] | skip]]] I have been stuck on this (in various forms) for over a week now |
BrianH 23-Jun-2009 [3092] | Well first of all, you have an extra [ ] in there, just after the some. |
mhinson 23-Jun-2009 [3093x2] | My thinking is that I expect the inner copy to be executed after the first "X" is found, then come back out of the inner bit when the next "X" is found. |
oops.. my bad with the extra [ ] I keep trying all sorts & that got left behind. | |
BrianH 23-Jun-2009 [3095x2] | parse/all { X X XX X X} [some [copy x "X" (prin x) [copy y "X" (print y) | skip] | skip]] Character at a time: - the outer skip - copy x "X" (prin x) - the inner skip - copy x "X" (prin x) - the inner skip - the outer skip - copy x "X" (prin x) - copy y "X" (print y) - the outer skip - copy x "X" (prin x) - the inner skip - copy x "X" (prin x) - the outer skip Try this: >> parse/all { X X XX X X} [some [copy x "X" (prin x) [copy y "X" (print y) | skip (prin "i")] | skip (prin "o")]] oXiXioXX oXiXo== true |
Now that last outer skip seems to me that it should be an inner skip, but I am clearly wrong :( | |
mhinson 23-Jun-2009 [3097] | Ah. that is a good trick to print i & o with the skips, this will help a lot, thanks. Isee from your analysis that my error is to expect the inner skip to skip back to the beginning of that loop... not sure why I expected that, but it is clearly wrong. I dont know if there is a way to make the inner loop behave like that, at an earlier point I did hve OPT in front of it... I will be able to make progress with my experiments now armed with the new trick you have taught me, but for now I have to go & get some sleep I am afraid. Thanks again. |
BrianH 23-Jun-2009 [3098x2] | >> parse/all { X X XX X X} [(prin 'a) some [(prin 'b) "X" (prin 'c) [(prin 'd) "X" (print 'e) | (prin 'f) skip (prin 'g)] (prin 'h) | (prin 'i) skip (prin 'j)] (prin 'k)] abijbcdfghbcdfghbijbcde hbijbcdfghbcdfijbik== true |
Now the fij is a bug in parse - it should be fgh. | |
sqlab 24-Jun-2009 [3100x2] | Excuse, if I did confuse you with my examples- I just tried to show you that you can get loops with an definite loop counter in parse. |
regarding parse/all "fd doixx s x x x oie x } " [some [copy d "x" (print d) | skip]] what did you expect? If you know what you are looking for you can extend it to parse/all "fd doixx s x x x oie x } " [some [copy d ["x" | "y" | "z" ] (print d) | skip]] and you will get your searched values. But maybe I just don't understand the problem. | |
mhinson 24-Jun-2009 [3102x4] | Thanks Brian, I am finaly getting it I think. the prin 'a etc is better than debugging techniquies I have tried because it is small and dosen't add too much to the complexity, I can see I could reduce this method further with something like a: does [prin 'a] |
Thanks sqlab, no need to excuse yourself please, your examples are great & I learnt a new use for COPY in PARSE. This has made your examples clear to me now, so thanks for spending your time helping me. The problem I have set myself is purely to understand parse more clearly so I have enough know-how to write any scripts I need without spending all day doing it. That is why I start off anking one question, then jump to another question if I don't fully understand the help I get. I have used parse a fair bit all ready, but limited myself to very simple concepts. see http://www.rebol.org/script-information.r?script-name=cisco-extract.r and marvel that it even works ;-) Thanks. | |
I have been reminded that |skip is a word | skip is OR skip. it is so easy to miss the space between | & skip | |
Right, I would say that the following snippit is the most educational thing I have done with PARSE. It shows me a lot of things about what is happening & validates the construction and use of charsets & whatever the 'address block is called. Thanks everyone for your help. digit: charset [#"0" - #"9"] address: [1 3 digit "." 1 3 digit "." 1 3 digit "." 1 3 digit] a: does [prin 'a] b: does [prin 'b] c: does [prin 'c] d: does [prin 'd] e: does [prin 'e] f: does [prin 'f] parse/all {1 23 4.5.6.12 222.1.1.1 7 8} [some[ (a) copy x address (prin x) some[ (b) copy y address break | skip (c)] (print y) | skip (d) ]] adadadadada4.5.6.12bcb222.1.1.1 | |
Graham 24-Jun-2009 [3106x2] | why not use block parsing? |
parse [ 1 23 4.5.6.12 222.1.1.1 7 8 ] [ integer! integer! tuple! tuple! integer! integer! ] | |
BrianH 24-Jun-2009 [3108] | Because he's parsing Cisco config files. |
mhinson 24-Jun-2009 [3109] | I am just learning everything I can, so knowing about Block Parsing sounds good to. tuples! are quite good for IP addresses, but ip addresses often need exactly 4 parts. I wonder why there are no datatypes specific for networking? e.g. networks & masks & inverse masks and domain names. and DNS records. |
Steeve 24-Jun-2009 [3110] | Mhinsob, if you load an ip address (from a string) you got a tuple, so what is different in your request ? >> type? probe load "1.2.3.4" 1.2.3.4 == tuple! |
Izkata 24-Jun-2009 [3111] | Possibly checking that it's a valid IP address? >> length? 1.2.3.4 == 4 >> length? 1.2.3.4.5 == 5 >> length? 1.2.3.44.5 == 5 |
Tomc 24-Jun-2009 [3112] | early tuples were more restrictive if I recall (max of lenght 4) but then we complained we wanted to use them for more than just networking |
BrianH 24-Jun-2009 [3113x2] | You can use up to 10 tuple elements - any higher wouldn't fit into an immediate value. |
I wonder why there are no datatypes specific for networking? In general we like our types to be more widely applicable. However, we have tuple!, url!, and port!. The rest can be handled by functions. | |
Paul 24-Jun-2009 [3115x3] | mhinson, not sure if your using R2 or R3 but if your using R2 then you might want to know about get-modes. |
>> get-modes tcp:// 'interfaces == [make object! [ name: "lo0" addr: 127.0.0.1 netmask: 255.0.0.0 broadcast: none dest-addr... >> print get-modes tcp:// 'interfaces name: "lo0" addr: 127.0.0.1 netmask: 255.0.0.0 broadcast: none dest-addr: none flags: [multicast loopback] name: "if15" addr: 169.254.102.14 netmask: 255.255.0.0 broadcast: 169.254.255.255 dest-addr: none flags: [broadcast multicast] name: "if12" addr: 192.168.1.105 netmask: 255.255.255.0 broadcast: 192.168.1.255 dest-addr: none flags: [broadcast multicast] | |
I don't know what the equivalent in R3 is. | |
BrianH 24-Jun-2009 [3118] | Nothing yet :( |
mhinson 25-Jun-2009 [3119] | The get-modes looks interesting. Can it produce information about any ip address I might give it? like 172.22.37.55/28 I suppose that is an example of a very common network address "type" |
older newer | first last |