World: r4wp
[Rebol School] REBOL School
older newer | first last |
Gregg 24-Apr-2012 [127] | Ah, there you go. Validate against your local and national rules and fail everything else. |
GrahamC 24-Apr-2012 [128] | yep |
Gregg 24-Apr-2012 [129x4] | Not sure if this will help, but maybe: |
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] ] | |
Example: parse-int-values "1234567890" [2 4 skip 2 2] | |
parse-int-values "(123) 456-7890" [#"(" 3 ") " 3 #"-" 4] | |
GrahamC 24-Apr-2012 [133] | http://en.wikipedia.org/wiki/Telephone_numbers_in_New_Zealand learnt something new .. 02 is for Scott Base in Antarctica ;) |
Gregg 24-Apr-2012 [134x2] | We had a REBOLer who worked there some years back. ...what was his name... |
G. Scott Jones? Now it's gonna bother me. :-\ | |
GrahamC 24-Apr-2012 [136] | yeah... though he won't be in Scott Base but the USA base there |
Gregg 24-Apr-2012 [137] | Still probably a local call from Scott Base though. ;-) |
GrahamC 24-Apr-2012 [138x3] | maybe not .. it may be routed thru the USA :) |
zero: charset [ #"0" ] digit: charset [ #"0" - #"9" ] digits: [ some digit ] local-number: charset [ #"3" #"4" #"6" #"9" ] not01-number: charset [ #"2" - #"9" ] national-rule: [ zero local-number not01-number digits] local-rule: [ not01-number digits ] emergency-rule: [ "111" ] ; toll free numbers are 0800 and 0508 toll-free: [ zero [ "800" | "508" ] digits ] ; directory assistance 018, 0172, directory-assistance: [ "01" [ "8" | "72" ]] ; faults 120, 125 and business 126 faults: [ "12" [ "0" | "5" | "6" ]] ; cellular are 021, 022, 027, 028, 029 cellular-rule: [ "02" digits ] ; international 00 - country code - number international-rule: [[ "00" | "+" ] digits ] allowed-phone-rule: [ local-rule | national-rule | emergency-rule | toll-free | directory-assistance | faults | cellular-rule ] all-phone-rule: [ allowed-phone-rule | international-rule ] | |
hope that covers all of my dialling rules | |
Endo 25-Apr-2012 [141] | Also check E164 formatting: http://en.wikipedia.org/wiki/E.164 and http://www.wtng.info/wtng-tt.html |
Arnold 26-Apr-2012 [142x2] | In PHP I had a Unix timestamp, which I didn't like, but it is in my database, so now I need this in my REBOL script too. |
Oh found a solution at http://www.rebol.net/cookbook/recipes/0051.html which works | |
Endo 26-Apr-2012 [144x2] | I use this function: Int2Date: func [i][1-Jan-1970/0:00 + to-time i] i is unix style date (seconds from 1970) you may need to change 0:00 to your GMT, if you don't store GMT separately. |
And for the oposite: Date2Int: func [d /local c][ c: 1-Jan-1970/0:00 d - c * 86400 + to-integer d/time - c/time ] | |
james_nak 26-Apr-2012 [146] | Christopher Gill's form-date.r is something I can't live without. http://www.rebol.org/view-script.r?script=form-date.r |
Endo 26-Apr-2012 [147] | I use my own functions, returns always in ISO format, YYYY-MM-DD HH:NN:SS padz: func ["Pad to right with zero." s n [integer!] /left /with c [char! string!]] [ head insert/dup either left [tail form s] [form s] first form either with [c] [#"0"] n - length? form s ] format-date: func [d [date!]] [ ajoin [d/date/year "-" padz d/date/month 2 "-" padz d/date/day 2 either d/time [ajoin [" " padz d/time/hour 2 ":" padz d/time/minute 2 ":" padz to-integer d/time/second 2]] [""]] ] |
Gabriele 27-Apr-2012 [148] | endo, Date2Int can be simplified as: >> to integer! difference now 1-jan-1970/0:00 == 1335506831 |
Endo 27-Apr-2012 [149] | That is much better. Thank you Gabriele! |
Gregg 27-Apr-2012 [150x4] | DIFFERENCE will fail for future dates, starting mid-January 2038. ;>> difference 19-jan-2038/3:14:07 1-Jan-1970/0:0:0 ;== 596523:14:07 ;>> difference 19-jan-2038/3:14:08 1-Jan-1970/0:0:0 ;== --596523:-14:-08 ;>> difference 19-jan-2038/3:14:09 1-Jan-1970/0:0:0 ;== -596523:14:07 |
Here's an old func I use: | |
date-to-epoch: func [ "Returns a unix time (epoch) format from a date." date [date!] ][ ; If no time is given, negate our zone to give us 0:00 UTC if none? date/time [date/time: negate now/zone] ; This uses the epoch base in UTC, so we assume that either ; the date is also in UTC, or has a zone offset included. ; DIFFERENCE fails for huge time differences, so we subtract ; them instead, giving us a difference in days, and multiply ; by the number of seconds in a day. either attempt [positive? res: to integer! difference date 1-Jan-1970/0:0:0] [res] [date - 1-Jan-1970/0:0:0 * 86400.0] ] | |
I believe the reason I still have DIFFRENCE in there, as far as it will work, is for greater accuracy. I don't remember why I have the zone negation bit in there, but I probably hit some issue that required it. | |
GrahamC 27-Apr-2012 [154x3] | @endo ... using math is fasting than padding with leading zeroes |
fasting => much faster | |
eg. next form 100 + now/day will give you 2 digits, and those less than 10 will have a leading zero | |
Endo 27-Apr-2012 [157] | Thank you guys. DIFFERENCE fails when it reaches integer limits, >> to-integer difference 19-jan-2038/3:14:08 1-Jan-1970/0:0:0 == -2147483648 >> ? -2147483649 == decimal! I think it will be a Y2K problem for Unix systems. |
Sunanda 27-Apr-2012 [158] | A similar problem has been reported in the past, and dismissed -- so not considered a bug by the developers: http://curecode.org/rebol3/ticket.rsp?id=1767&cursor=12 |
Endo 27-Apr-2012 [159] | In Carl's example one of the numbers should be decimal, otherwise it gives overflow error: 24.0 * 60 * 60 * subtract 1-jan-2000 1-jan-2100 or 86400.0 * subtract 1-jan-2000 1-jan-2100 |
Gabriele 27-Apr-2012 [160] | R2 is only 32 bits :-) |
Evgeniy Philippov 28-Apr-2012 [161x4] | Could anyone tell, Is rebol a language for sketching systems, or is it a full-fledged Turing-full language with access to all machine's resources? Seems to be a sketching one, IMHO. Could anyone comment on this? |
Not intending to start a religious war, but it's interesting what opinions about such and other qualities of rebol people have. | |
I have almost zero experience at programming in rebol. | |
Or is Rebol an OverForth type language which is too syntactically *liquid* to use in long-term industrial and long-term stable projects? Could anyone comment? | |
Gregg 28-Apr-2012 [165] | REBOL is a full-fledged, high-level language. I use it for commercial work. R2 is very stable, but does have a small number of things that may never be fixed. Only you can say if they would cause you problems. The bigger issue is that RT doesn't seem to be maintaining REBOL anymore. The hope is that Red, World, and others will mature enough to be viable options. |
Henrik 28-Apr-2012 [166] | rebol a language for sketching systems - nevertheless, REBOL is also excellent for prototyping things that need to be done in another language. |
Sunanda 28-Apr-2012 [167] | REBOL is structured more like LISP or Haskell (but without being a pure functional language). So, yes, it does not have direct access to the machine instructions or low-level op sys APIs in the way that assembler or assembler-wrapper languages (like C) does. What REBOL does have is easy integrated access to very high level APIs: parse. bind, map, etc. |
Sujoy 3-May-2012 [168] | beginner question: i'm opening a file using d: open/direct/lines %bigfile.nt i am then looping through each line using: while [ln: first d] [ ;do something here ] i need to record the byte position of the start and end of each line... how? |
PeterWood 3-May-2012 [169] | I think you will have to calculate them yourself using length? ln and adjust for newline/cr as appropriiate. |
Sujoy 3-May-2012 [170x3] | thanks Peter... i can find the length? of each line, but how do i calculate the length of the newline? |
>> length? to-string #"^/" == 1 >> length? to-string crlf == 2 | |
how do i detect the newline used in %bigfile.nt? | |
Maxim 3-May-2012 [173] | hahaha I was writing exactly about this. |
Sujoy 3-May-2012 [174] | :) |
Maxim 3-May-2012 [175x2] | basically... just find crlf in the file. |
if you find one, you can assume its using crlf format for newlines. | |
older newer | first last |