----- Original Message -----
From: <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Saturday, February 19, 2000 11:15 AM
Subject: [REBOL] Parse questions ??


> Hello All,
>
> I have a file consisting of lines in the following format:
>
> "23123+34234+234234-23423+3-"
>
> ie, n digits followed by a sign etc etc.
>
> The only way i've been able to extract the data into a series is to do two
> replaces, replacing "+" by "+ " and "-" by "- " and then parsing it using
> the space as a delimiter. I can't use the sign as a delimiter because that
> only returns the value and not the sign.
>
> My first question is, should parse allow the delimited character to be
> optionally returned by way of a refinement?? In this case the sign is a
> delimiter but also an integral part of the data.

Hi Mike,

You are using parse in its simplist form, as just a split function,
whenever you need more from it, you can create your own parse rules.
See attached script, and the 'parse topics in the docs
>
> The other problem i had was once i had the data in the form "123+", i
> wasn't able to convert it to decimal, because the sign was at the end and
> not the beginning, so i had to write code to move it.
> Do people think that decimal and integer should be modified to allow for a
> optional sign at the end??

I admit I don't see this form very often, so I would not see any need for it
to be built in,
but others may. It only takes one or two lines to convert this format to an
integer, it doesn't
take much to make your own converter. (see attached script)

Cheers,

Allen K



REBOL []

to-integer-block: func [
    {Converts data "23123+34234+234234-A23423+3-" into a block of signed integers
     Skips/ignores illegal chars} 
    string [string!] 
    /local item list signs numeric rule convert
][
    ;--- convertor
    convert: func [
        {Converts end signed strings "4567-" to integer -4567}
        string [string!]
    /local number sign
    ][
        set [number sign] reduce [(copy/part string (length? string) - 1) last string] 
        to-integer join sign number 
    ]

    item: copy ""
    list: copy []
    signs: to-bitset [#"-" #"+"]
    numeric: to-bitset [#"0" - #"9"]
    rule: [some [[copy item [some numeric signs] (append list convert item)] | [none 
skip]]]
    parse string rule 
    return list
]

 
to-integer-block "23123+34234+234234- ABCDE- 23423+3-"
== [23123 34234 -234234 23423 -3]

Reply via email to