• Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 

World: r4wp

[Rebol School] REBOL School

Endo
28-May-2013
[2007x2]
I see, I just confused if it's a bug or not.
>> load-xml {<a>test</a>}  ; == [ <a> "test" ]

>> load-xml {<a b="c">test</a>}   ; == [ <a> [ #b "c" %.txt "test" 
] ]
Why this doesn't work?
>> parse [1] [1] ;== false

while this one works
>> parse [a] ['a] ; == true
Cyphre
28-May-2013
[2009]
parse uses literal numbers for rules definition
you need to use: parse [1][integer! (process-integer-here)]
Endo
28-May-2013
[2010]
So, no chance to specify an exact number in a parse rule?
Geomol
28-May-2013
[2011]
>> parse [1] [1 1 1]
== true
>> parse [1 1] [2 2 1]
== true
Maxim
28-May-2013
[2012]
darn... never new we could do that!!!!  logical enough... wonder 
why I never tried it.
Andreas
28-May-2013
[2013]
In R3, youi can also use the QUOTE command:

>> parse [1] [quote 1] 
== true
Gregg
28-May-2013
[2014x3]
parse-int-values: func [

    "Parses and returns integer values, each <n> chars long in a string."
    input [any-string!]

    spec [block!] "Dialected block of commands: <n>, skip <n>, done, 
    char, or string"
    /local
        gen'd-rules ; generated rules
        result      ; what we return to the caller

        emit emit-data-rule emit-skip-rule emit-literal-rule emit-data
        digit= n= literal=
        int-rule= skip-rule= literal-rule= done= build-rule=
        data-rule skip-rule
][

    ; This is where we put the rules we build; our gernated parse rules.
    gen'd-rules: copy []
    ; This is where we put the integer results
    result: copy []

    ; helper functions

    emit: func [rule n] [append gen'd-rules replace copy rule 'n n]
    emit-data-rule: func [n] [emit data-rule n]
    emit-skip-rule: func [n] [emit skip-rule n]
    emit-literal-rule: func [value] [append gen'd-rules value]
    emit-data: does [append result to integer! =chars]

    ; Rule templates; used to generate rules

    ;data-rule: [copy =chars n digit= (append result to integer! =chars)]
    data-rule: [copy =chars n digit= (emit-data)]
    skip-rule: [n skip]

    ; helper parse rules
	digit=: charset [#"0" - #"9"]
    n=: [set n integer!]
    literal=: [set lit-val [char! | any-string!]]

    ; Rule generation helper parse rules
    int-rule=: [n= (emit-data-rule n)]
    skip-rule=: ['skip n= (emit-skip-rule n)]
    literal-rule=: [literal= (emit-literal-rule lit-val)]
    done=: ['done (append gen'd-rules [to end])]

    ; This generates the parse rules used against the input

    build-rule=: [some [skip-rule= | int-rule= | literal-rule=] opt done=]


    ; We parse the spec they give us, and use that to generate the

    ; parse rules used against the actual input. If the spec parse

    ; fails, we return none (maybe we should throw an error though);

    ; if the data parse fails, we return false; otherwise they get
    ; back a block of integers. Have to decide what to do if they
    ; give us negative numbers as well.
    either parse spec build-rule= [
        either parse input gen'd-rules [result] [false]
    ] [none]
]
>> parse-int-values "123456" [1 2 3]
== [1 23 456]
Just as an example of how you can work around it with a bit of indirection.
Sujoy
7-Jun-2013
[2017]
this is on r3
am trying to do a simple 
read http://google.com
and get 

Access error: protocol error: "Redirect to other host - requires 
custom handling."
how do i custom handle?
Endo
7-Jun-2013
[2018]
I think you should use cURL binding for R3.
GrahamC
7-Jun-2013
[2019x3]
@Sujoy .. there some relatively easy fixes to the R3 http protocol 
to handle this.  But .. due to various issues .. they just have not 
found their way into the binaries :(
Basically the http protocol sees a redirect eg. from http:// google 
to https google and complains.
or from google.com to www.google.com etc.
Sujoy
7-Jun-2013
[2022x3]
thanks graham. 
endo - looking at cURL...thanks for the pointer
>> import %./cURL-binding.so

** access error: cannot open: %./cURL-binding.so reason: "not found 
or not valid"
any ideas why? i'm on a amazon linux instance...
GrahamC
7-Jun-2013
[2025]
Have you installed all the libraries required for cURL?
Sujoy
7-Jun-2013
[2026x2]
i think so graham - did a yum install curl
and i can curl http://google.comfine
GrahamC
7-Jun-2013
[2028]
Guess have to ask Kaj then ... I tried this before and worked fine 
for me
Sujoy
7-Jun-2013
[2029]
thanks graham...hoping kaj will see this...
Endo
7-Jun-2013
[2030]
Here you can find some info:
http://rebol.esperconsultancy.nl/extensions/cURL/
Kees
17-Jul-2013
[2031]
Question about an example from the R3 docs:

str: "abcdef"
end: find str "d"
for s str end 1 [print s]
abcdef
bcdef
cdef
def


find finds the d at position 4, if I replace end with 4, I get the 
same result.
However: type? end says string! and no pointer

If I replace the text in str, end still equals to "def", so it does 
not point
at str any more.
Can someone explane this?
Pekr
17-Jul-2013
[2032x2]
'find returns the string at certain position, just print 'end, and 
you will obtain "def"
your 'for construction looks strange, I am wondering it works :-)
Kees
17-Jul-2013
[2034]
It does, both in R2 as in R3, this is from http://www.rebol.com/r3/docs/guide/code-loops.html#section-3
DideC
17-Jul-2013
[2035x4]
In Rebol, there is no "pointer" (C like).

string!, binary! are series. Series are groups of elements (character, 
octet) so a word! (like str or end) associated to a serie hold also 
a position on it.
You can mak as many sords you want pointing to the same serie at 
the same or different position.
mak=make  sords=words (sory)
Understanding your cade form the interpreter point o view :
Kees
17-Jul-2013
[2039]
How can I find out that the for loop exchanges end with 4, exept 
for looking it up myself ?
DideC
17-Jul-2013
[2040]
str: "abcdef"

==> Create a string! in memory, put "abcdef" as its content, create 
a word! 'str an make it point to its head.
Pekr
17-Jul-2013
[2041x3]
hmm, I just tried for i 1 str 1, and it screams ... but maybe if 
given the same type, a string for e.g., maybe it takes their index 
value?
>> index? str
== 1
>> index? end
== 4
you should know, that 'end is just a reference to still the same 
str, which can be proven, by inserting new element into str ....
Kees
17-Jul-2013
[2044]
Yes, index? does it! Thanks Pekr
DideC
17-Jul-2013
[2045]
end: find str "d"

==> search for "d" in the "abcdef" series in memory, then create 
a word! 'end that hold the position of "d" in this same serie.
Pekr
17-Jul-2013
[2046]
>> insert str "0"
== "abcdef"
>> str
== "0abcdef"
>> end
== "cdef"
DideC
17-Jul-2013
[2047]
so 'str and 'end just hold different position in a unique string!
Pekr
17-Jul-2013
[2048]
good point, DideC
Kees
17-Jul-2013
[2049]
Tried that myself, but no change in end, which Rebol version are 
you using?
DideC
17-Jul-2013
[2050]
Then, the 'for loop can work as it's positions in a unique serie!
Pekr
17-Jul-2013
[2051x3]
2.7.8
Kees - beware - rebol series concept needs really carefull aproach 
- it caused me a headache when working with series, till I became 
accustomed to it. And still, sometimes, I use trial and error aproach 
in console ...
Kees - better start with a fresh session, fresh series ....
DideC
17-Jul-2013
[2054]
If I replace the text in str, end still equals to 
def", so it does not point
at str any more."
How do you replace the text in 'str ?

If it's like this:
	str: "new text"

then you have created a new string! in memory and point 'str to this 
new serie. 'str and 'end does not point anymore the same string!
Pekr
17-Jul-2013
[2055x2]
try on one line in console:


>> str: "abcdef" end: find str "d" print end insert str "0" print 
end
def
cdef
Kees: there are 'replace and 'change functions ....