r3wp [groups: 83 posts: 189283]
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 

World: r3wp

[I'm new] Ask any question, and a helpful person will try to answer.

Sunanda
17-Jul-2007
[553]
Try this:

 rejoin [now/year "-" now/month "-" now/day "-" first now/time "." 
 second now/time "." thir
d now/time/precise "000" ]

But you may need to add some more trailing zeroes.... a time of 01:02:03.100 
would show in REBOL as 1:2:3.1
PatrickP61
17-Jul-2007
[554]
That seems to work -- except for adding leading zeroes which can 
be done via above script -- Thanks Sunanda
Gregg
17-Jul-2007
[555]
I have a format func that isn't on REBOL.org (yeah, I know...; it 
requires another func, etc.) if you have to do a lot of formats and 
don't want to roll them all. Anyway, let me know if you want me to 
send it Patrick.
PatrickP61
17-Jul-2007
[556x2]
Sure -- Why not  -- I'm learning more and more all the time
OK -- I'm perplexed as to when does things get evaluated.

If I have a variable like Now-TS: to get the formatted time, it will 
be resolved immediately and return the time.

If later, after I wait 1 second, I want to print the new formatted 
timestamp, it returns the exact same value as before, when I know 
the time has acutally changed.  How do I get the time now to be resolved 
again?  Example code:

print now/precise		gives 17-Jul-2007/14:35:21.308-5:00 
 wait 1

 print now/precise		gives 17-Jul-2007/14:35:22.324-5:00	now/precise 
 is evaluated immediately

 Now-timestamp: 
	rejoin [
		Now/year "-" Now/month "-" Now/day "-" 
		first Now/time "." second Now/time "." third Now/time "000" ] 

 print Now-timestamp			gives	2007-7-17-14.35.22.0000	
 wait 1 

 print Now-timestamp			gives	2007-7-17-14.35.22.0000		the exact same 
 time -- not evaluated immediately


Is it this way because Now-timestamp has been assigned and already 
evaluated  -- if so, how do I have it reevaluate it again?
BrianH
17-Jul-2007
[558]
Wrap it in a function.
now-timestamp: does [rejoin [...]]
PatrickP61
17-Jul-2007
[559x2]
Ok, so if a variable is unset, then it is evaluated when defined.

If it is already defined, then it is not evaluated again unless there 
is a do or does?  Is that right?
Super -- that worked just great
BrianH
17-Jul-2007
[561x4]
DOES is a shortcut for creating a function, DO evaluates its value 
directly. A variable is not evaluated when assigned - the value is, 
and then it is assigned to the variable. You don't really "define" 
variables in REBOL, but the distinction may be more complicated than 
you need to worry about for now.
You might consider that the time will march on during the course 
of your evaluation, so you might want to store it in a local variable, 
like this:


pad0: func [x n [integer!]] [head insert/dup (x: form :x) "0" (n 
- length? x)]
now-timestamp: func [/local n] [n: now/precise rejoin [

    pad0 n/1 4 "-" pad0 n/2 2 "-" pad0 n/3 2 "-" pad0 n/4 11 "000"
]]
Sorry, that won't work in some cases. Try this instead:

now-timestamp: func [/local n s] [
    n: now/precise
    s: n/4/3
    s: join either s < 10 ["0"] [""]
    s: head insert/dup tail s "0" 9 - length? s
    rejoin [
        pad0 n/1 4 "-" pad0 n/2 2 "-" pad0 n/3 2 "-"
        pad0 n/4/1 2 ":" pad0 n/4/2 2 ":" s
    ]
]
missing an s :(

s: join either s < 10 ["0"] [""] s
PatrickP61
17-Jul-2007
[565x3]
Thanks Brian.  I will play around with it a little more.  Just to 
re-iterate my understanding of rebol assignments


A variable is not evaluated when assigned - the value is, and then 
it is assigned to the variable. You don't really 
define" variables in REBOL"

So at the time of assignment, the text following the : is assigned 
to the variable but is not evaluated.  That is to say the variable 
is like a pointer to the text string that was typed in.

Does that mean that Rebol will not do evaluations until it needs 
to.  For example:
In-file:	%file_path_name.txt
In-text:	Read In-file

write %out-file-path-name.txt In-text		<-- this is where the evaluation 
occurs to resolve all the above?  Is that right?
Another similar example:

In-file:	%file_path_name.txt
In-text:	Read In-file

append In-text 'this-is-the-end-of-the-file	<-- evaluated because 
of action word append
write %out-file-path-name.txt In-text
is that right?
BrianH
17-Jul-2007
[568x4]
That's not what I meant. I meant that the expression to the right 
of the set-word (s:) is evaluated. The result of that evaluation 
is the value that will be assigned to the word. So, you were right 
the first time about the evaluation order.
The word itself is not evaluated though, it is just assigned. The 
value that the word was assigned is returned from the assignment 
expression too, so that you can chain assignments or use the value 
later, like I did in pad0 above.
Later on you can either do a full evaluation of the word by stating 
it directly ( a ) or you can just retrieve its value by using a get-word 
( :a ).
REBOL variables don't really need to be declared, as such, but you 
do need to declare function parameters and object fields. Some of 
the REBOL language look like they are declaring variables, but they 
really are doing something different.
PatrickP61
18-Jul-2007
[572]
Thanks for your patience with me.  I'm wrong about the evaluation. 
 It is done at the time of the assignment returing whatever value 
to the variable.  The reason Now-timestamp had identical values, 
even after waiting 1 second was that it was evaluated once, with 
a value put into it, then the wait happened, then I simply re-printed 
the same value as before, because I did not re-do the variable.  
I think I was making it harder than it really is.

I don't understand this statment:

Later on you can either do a full evaluation of the word by stating 
it directly ( a ) or you can just retrieve its value by using a get-word 
( :a )


Are you saying that I can simply type Now-timestamp to have it re-evaluated 
at that time?
btiffin
18-Jul-2007
[573x7]
Umm, not quite.  You're getting into it now.  :)  Sometimes it helps 
to think of it this way (but it is actually 'deeper')    myprint: 
print   won't work, it tries to evaluate print, but myprint: :print 
 "gets" the value of print and then you can  myprint [1 2 3]   - 
again it's deeper than what I just explained.
a: 23 * 56   when interpreted will compute 23 * 56 then the set-word 
 a:  assigns the value 1288 to the variable a.  Because it is just 
a number  a  and  :a  are both 1288, you can't really get at the 
23 * 56 anymore, that expression has been evaluated and "forgotten", 
forgotten not really a good word, but the expression 23 * 56 is not 
around anymore, only the 1288.
Patrick; I just looked back a little bit, your question about formatted 
time-stamps...Chris has donated an awesome date time formatter to 
the rebol.org repository.  Very close to strftime in function.  Check 
out


http://www.rebol.org/cgi-bin/cgiwrap/rebol/view-script.r?script=form-date.r
form-date now/precise "%C%y-%m-%d-%H:%M:%S"  doh!  loses the precise, 
so...

dt: now/precise  t: dt/time  rejoin [form-date dt "%C%y-%m-%d-%H:%M:%S" 
find form t/second "."]


again, not quite right...the precision won't be right padded with 
zeros, one more step
dt: now/precise   t: dt/time   t: find form t/second "." 

rejoin [form-date dt "%C%y-%m-%d-%H:%M:%S" head change/part ".000000" 
t length? t]

I think I'll bug Chris to add %P for precise padded seconds.  :)
and of course   head change/part COPY ".000000" ... if you reuse 
the sequence...It'd be nice to be able to just use form-date now/precise 
"%C%y-%m-%d-%H:%M:%P"
Patrick;  Never mind for now...the %S is rounded, won't work.  Sorry 
for the interruption.
PatrickP61
18-Jul-2007
[580]
btiffin -- Thank you for taking the time to explain it.  I think 
I understand it now.  I was initially confused because I tried to 
print a timestamp knowing full well that time is changing and I didn't 
understand how to get it evaluated.  I confused the assignment of 
a value with that of a function..  Good info on the date timestamp 
above.

Thank you all!
Gregg
18-Jul-2007
[581x2]
If you read the Core manual on REBOL.com, it has a pretty good explanation 
of the four word types (normal, lit, set, and get). The other thing 
to understand is when blocks, and nested blocks, are reduced (evaluated). 
That can be tricky to figure out sometimes, because funcs like PRINT 
do it automatically.


If you can get a handle on when things are evaluated--and don't stress 
when you have to add a REDUCE or COMPOSE but aren't sure why--and 
if you can grok the four word types, you 'll be in great shape.
When getting started, you can quite often treat REBOL like many other 
languages; it has a nice facade to let you get a lot done that way 
without forcing you to understand how it really works.
btiffin
19-Jul-2007
[583]
rebol.org %form-date.r updated...

Fix for time-stamps and a really nice short-cut that includes the 
zone.
%c
 outputs all the fields of now/precise  nicely formatted
%s
 outputs the seconds with nanosecond precision  nicely formatted

form-date now/precise "%C%y-%m-%d-%H:%M:%s" is now all you need for 
IBM DB2 time-stamps.
Graham
19-Jul-2007
[584]
javascript dates??
btiffin
19-Jul-2007
[585]
Whoey whatty?  :)  These are all zero-padded.  Addition of the "%#H" 
type sequences wouldn't be too-too hard.
Graham
19-Jul-2007
[586x2]
javascript dates are milliseconds since 1-Jan-1970
doing this in math causes a few problems!
btiffin
19-Jul-2007
[588x2]
We decided to use %s for REBOL precision...no epoch seconds in form-date. 
 And yeah, I was looking at the TIME and NTP protocols...it would 
not be fun in high level REBOL, I don't think even Rebcode could 
poke the right fields for time!
Moving to Library...
PatrickP61
19-Jul-2007
[590]
Hi all -- I didn't mean to cause additional entries to other libraries 
-- just tried to format it in my own routine.  But since there is 
interest, I double checked the official IBM SQL reference manual 
for timestamps.

So for the record and for your information:


Although there are many many different forms (Japanese, Europe etc), 
I only focused on two elements, the ANSI  ISO timestamp, and IBM 
SQL timestamp standard.  So in the IBM book "DB2 UDB for iSeries 
SQL Reference V5R370" under "Data Types, Datetime values, table 10 
page 70":

_________________________________________________________________________________________________

Table 10. Formats for String Representations of Timestamps
Format Name			Time Format						Example

ANSI/ISO SQL standard	TIMESTAMP ’yyyy-mm-dd hh:mm:ss.nnnnnn’	TIMESTAMP 
’1990-03-02 08:30:00.010000’

IBM SQL			’yyyy-mm-dd-hh.mm.ss.nnnnnn’			’1990-03-02-08.30.00.010000’
14–character form		’yyyymmddhhmmss’					’19900302083000’

_________________________________________________________________________________________________


For the record, I think I confused the two types.  Notice the embedded 
space between the date and time as well as : separators for ANSI/ISO, 
while the IBM SQL standard contains it all (no embedded space) and 
uses periods to separate the time elements, which I will easily fix 
in my version. -- You may wish to do the same for any new form of 
date you have.
btiffin
19-Jul-2007
[591]
Patrick;  No problem about library entries.  :)  When REBOL grows 
we all benefit.  Nothing in rebol.org is 'official'.  It's a user 
maintained repository of 'stuff'.  Chris' form-date just happens 
to be one of the beauties.  It is close to but not the same as the 
C strftime function.  With form-date you can make up pretty much 
any date time output you'd like.  No one has to use form-date, I 
was just cheerleading.  So I'll cheerlead a liitle bit.  I you haven't 
yet, check out http://www.rebol.org.Sunanda and team have created 
a a world class repository of information and functionality that 
is all REBOL user community generated.
PatrickP61
19-Jul-2007
[592]
Thanks btiffin -- Yes I agree that having a library of common routines 
is the way to go.  I'm new and just learning how to play with Rebol 
with the approach of -- If I do it this way, what will rebol do, 
rather than, use the library to find common routines, and use it, 
which of course I would do to solve a specific problem.  Rebol.org 
is a great resource!
btiffin
19-Jul-2007
[593]
Nice approach.  REBOL really does allow for exploration, and when 
puzzles are seen from an angle that was not even considered before 
is when the magic can happen.  :)
PatrickP61
19-Jul-2007
[594]
Hi all,  As you may have guessed from my above posts, I'm trying 
to write a script that will convert a formatted report into a table 
or CSV.  I'm new and just playing around to understand the process. 
 


In any event, I did search rebol.org on CSV and found the CSV.r script 
which seems to a part of what I would like to do.


But here is my concern.  The Mold-CSV function does not handle all 
the different kinds of strings that can occur.  I'm talking about 
embedded " or {.  I would like a function that can handle all strings 
properly into CSV.

Take this example:
 In-block:	[
	[	"C A1"	"C B1"	] 
	[	2		3		] 
	[	"2"		"3"		] 
	[	"4a"    	"4b"	]
	[	"5a x" 	"5b y"	]
	[	""7a,""	""7b,""	]
	[	""a8"""	""b8"""	]	
	]


Mold-CSV In-block doesn't handle 7a or a8 lines properly since the 
"" terminates a string.  You could replace the first and last " with 
a brace {} but that does some funny things too.
Henrik
19-Jul-2007
[595x2]
those should be converted to ^" and ^{ ^}, I believe
>> "^""
== {"}
PatrickP61
19-Jul-2007
[597]
Working in reverse, If you go into Microsoft Excel and type in your 
desired contents, you will see how they do it:

- If a cell has embedded commas, then " are put around the entire 
cell contents.

- If a cell has embedded " like  abc"def, then the " is repeated 
as "abc""def".

just food for thought
btiffin
19-Jul-2007
[598]
Patrick;  This is pretty common with a lot programming, escaping 
quotes inside strings.  And Henrik, just beat me to it and offered 
up a starting point...  :)
PatrickP61
19-Jul-2007
[599x2]
Henrik, are you saying that I should change any embedded " to ^"? 
 Like [ "^"7a,^"" ]
.
Henrik
19-Jul-2007
[601]
that would do it, yes
PatrickP61
19-Jul-2007
[602]
I'll try it!