[REBOL] Parse questions ?? Re:(2)

2000-02-20 Thread mdb

Thanks Gabriele,

I was wondering where the to-integer was, but then i realised that the
(value: load value) took care of it.
Interesting that you looked at the data in terms of repeating groups and so
processed them using some..., when i came up with a parse rule, i tried to
use recursion and i think that caused problems for me.

Mike.

- Original Message -
From: [EMAIL PROTECTED]
To: [EMAIL PROTECTED]
Sent: Saturday, February 19, 2000 2:50 AM
Subject: [REBOL] Re: Parse questions ??


 Hello [EMAIL PROTECTED]!

 On 19-Feb-00, you wrote:

  m I have a file consisting of lines in the following format:

  m "23123+34234+234234-23423+3-"

  m ie, n digits followed by a sign etc etc.

 What about:

  data: "23123+34234+234234-23423+3-"
 == "23123+34234+234234-23423+3-"
  values: make block! 10
 == []
  digits: charset "0123456789"
 == make bitset! #{
 FF03
 }
  parse data

 [some

 [copy value some digits
 [(value: load value)
 [["+" (insert tail values value) | "-" (insert tail values negate
value)]
 []
 []
 == true
  values
 == [23123 34234 -234234 23423 -3]

 Regards,
 Gabriele.
 --
 o) .-^-. (--o
 | Gabriele Santilli / /_/_\_\ \ Amiga Group Italia --- L'Aquila |
 | GIESSE on IRC \ \-\_/-/ /  http://www.amyresource.it/AGI/ |
 o) `-v-' (--o




[REBOL] Parse questions ?? Re:

2000-02-18 Thread allenk


- 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]