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.

btiffin
18-Jul-2007
[576x4]
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
[602x3]
I'll try it!
Getting better, but still no cigar.


Here is my test code for Mold-CSV function in CSV.r script:  ( I 
hope this formats correctly on Altme)

In-block:	[					; (want in csv file)	; (want in excel) 
["Col A1"		"Col B1"		]	; Col A1,Col B1	; Col A1	Col B1
[2			3			]	; 2,3			;      2	     3
["'3"			"'4"			]	; '3,'4			; '3		'4
["4a"			"4b"			]	; 4a,4b			; 4a		4b

["^"5a^"^"^"^""		"^"^"^"5b^"^""		]	; "5a""""","""5b"""	; 5a""		"5b" 
["6a x"			"6b y"			]	; 6a x,6b y		; 6a x		6b y
["7a, x"			"7b,^"^" y"		]	; "7a, x","7b,"" y"""	; 7a, x		7b," y"

["^"8a ^"^",x"		"^"8b ^"^"^"^"y^""	]	; "8a "",x","8b """" y"	; 8a 
",x	8b "" y

["^"^"^"9a^"^" x"		"^"9b ^"^"y^"^"^""	]	; """9a"" x","9b ""y"""	; 
"9a" x	9b "y" 
]

 Out-block:	Mold-CSV In-block
 write %Book2.csv Out-block
____________

In the above, I have 3 "views" if you will of what I am after.

The first view is the In-block that I would like Mold-CSV to run 
against.

The second commented view is what I need Mold-CSV to generate to 
put into the csv file

The third commented view is what Microsoft Excel will generate when 
I open the CSV file.


Mold-CSV works fine for the first 6 lines, then it gives me this 
for lines 7,8 and 9:

7a, x
,{7b,"" y}		<-- Where did the braces come for for 7b?
{"8a "",x},"8b """"y"	<-- Same quest for 8a?

9a
 x
,"9b ""y"""	ok

Any ideas on how to solve this?
Correction:	Where did the braces come from for 7b?
Anton
19-Jul-2007
[605x6]
Rebol molds long strings, or strings which contain a quote " inside 
{ } instead of " ".
Watch this:
>> "{^""
== {^{"}
The string I typed above contains an open brace and a double-quote 
(which I've escaped).

Rebol probably saw that the string contains an escaped quote and 
decided to mold it with { } to avoid having to escape it.
However, now the opening brace { needs to be escaped.
In short, Rebol's molding of strings can be in two different formats 
depending on the input string, and depending on where you get your 
input string from, it can be hard to guess which one it is.

I would suggest to make your own Excel/CSV-string-molding function 
to ensure you have double-quotes as expected.

Other people have come to this exact same problem before, by the 
way.
I don't understand the exact logic of the CSV file quote formatting, 
but you could use a function similar to this:
	enquote: func [string][rejoin [{"} string {"}]]
>> print enquote "hel^"lo"
hel
lo"

and you could take it further by replacing single instances of " 
in the string with two instances, etc.
Hmm.. 

http://www.rebol.org/cgi-bin/cgiwrap/rebol/view-script.r?script=csv.r

Mold-CSV just uses MOLD, so you should replace it with your own MOLD-CSV-STRING 
function, similar to the above enquote.
PatrickP61
19-Jul-2007
[611]
Anton, Is MOLD-CSV-STRING different than the MOLD-CSV in the above 
link?  I don't see it inside of that script.
Anton
19-Jul-2007
[612x5]
mold-csv-string: func [string][rejoin [{"} replace/all copy string 
{"} {""} {"}]]
With the link I was just verifying that we were talking about the 
same script in rebol.org.
MOLD-CSV uses rebol's built-in MOLD to mold the string. I am proposing 
to replace MOLD with the above one-liner MOLD-CSV-STRING function.
Just replace the two instances of MOLD with MOLD-CSV-STRING.
Wait a minute ! MOLD handles any value, while MOLD-CSV-STRING only 
handles series at the moment... hang on.
PatrickP61
19-Jul-2007
[617]
Anton,  What do you think of this approach -- I'm just thinking it 
through and am not sure if I have covered all the bases.

Since my input can contain any number of symbols, commas, single 
and double quotes, and rarely, but possibly braces, what if I attack 
the problem a different way.  Whenever an embedded comma, or double 
quote or something like that occurs within an spreadsheet cell, it 
will require some kind of "extra" formatting like two quotes or the 
like.  It may even be that there are unique combinations of such 
symbols, rare as that would be, to have complex formatting of an 
input block for rebol to convert it properly for CSV.


What if I shift gears and look at a TAB delimited file instead.  
I know that I will never have TAB embedded in my cells, and that 
I deal with the entire block as a series instead.  I could embed 
TAB wherever needed to separate the columns and leave the remaining 
string the way it is.  Would that work, or would I still need to 
do some formatting to handle it.  I think I'll open an excel spreadsheet, 
and work in reverse to see what it needs for TAB delimited file. 
 Any comments?
Anton
19-Jul-2007
[618x2]
That may alleviate the string quoting problem. Worth a try.
I think I got it.

mold-csv-string: func [value][append insert replace/all either any-string? 
:value [copy :value][mold :value] {"} {""} {"} {"}]
PatrickP61
20-Jul-2007
[620x4]
Anton,  I tried the mold-csv-string and got the following:
[
    [
Col A1
 
Col B1
] 
    [2 3] 
    [
'3
 
'4
] 
    [
4a
 
4b
] 
    [{
5a



} {



5b
}] 
    [{

6a x

} 
6b y
] 
    [
7a, x
 {7b,

 y

}] 
    [{
8a 

,x} {
8b 



y
}] 
    [{


9a

 x
} {
9b 

y


}]
]

Which is not usable for excel  -- Maybe I mis-understood how to use 
it...
I also checked out tab delimited and the rules for that is much much 
simpler.

If a quote or comma is embedded in the string and does not start 
with a quote, then I can leave the value as it is.

The only thing I need to handle is if a value starts with a quote, 
then I need to add additional " around it, which I should be able 
to do!
For those of you monitoring and want to see what I mean:

Out-string:	[		; (want in tab file)		; (want in excel)
	{Col A1^-Col B1^/}	; Col A1	Col B1		; Col A1	Col B1 
	{2^(tab)3^(line)}	; 2		3		;      2		     3
	{'3^-'4^/}		; '3		'4		; '3		'4
	{4a^-4b^/}		; 4a		4b		; 4a		4b
	{5a""^-"""""5b"^/}	; 5a""		"""""5b"		; 5a""		""5b
	{"""6a x"""^-6b y^/}	; """6a x"""	6b y		; "6a x"		6b y
	{7a, x^-7b," y"^/}	; 7a, x		7b," y"		; 7a, x		7b," y"
	{8a ",x^-8b "" y^/}	; 8a ",x		8b "" y		; 8a ",x		8b "" y
	{"""9a"" x"^-9b "y"^/}	; """9a"" x"	9b "y"		; "9a" x		9b "y"	
]

 write %Book2.txt Out-string 


Aside from just need to insert a ^(tab) or ^- at the appropriate 
places to separate a cell from each other, and a ^(line) or ^/ in 
a string, I will also need to check the first character of each "cell". 
 If it starts with a ", then I need to add another set around it. 
 See 5b and 9a above to see what I mean.
-- (I wish AltMe would show the same spacings / tabs that I type 
into the New Message box as it would appear on the postings )
Henrik
20-Jul-2007
[624x2]
I'm not really sure what you should use there, because it seems to 
me that rebol always reduces ^" to ", unless you enter them by hand.
also I don't know if I understand your problem correctly. it's a 
bit hard to follow. :-)