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

World: r3wp

[Rebol School] Rebol School

Steeve
8-Mar-2010
[2912]
Yeah sort of Mental Health.

Even after all these years I still check the list of all words, regularly.
And I still make discoveries.
My last one ?
TRY/EXCEPT
PatrickP61
8-Mar-2010
[2913x5]
Hi all, I have a problem to figure out.


I have a special needs child that uses a talker device to speak for 
her.  It will log all of the words buttons she pushes to a file. 
 Problem is, the timestamp was not adjusted to the correct time and 
date, as a result each record with a timestamp is off by 6 years, 
3 months, 25 days, 6 hours and 17 minutes.  The format of the file 
is like this:

30th 6pm
*[YY-MM-DD=03-06-30]*
18:04:38 RECORD ON
22:55:13 CTL "Switch User Area from ..."
...
*[YY-MM-DD=03-07-01]*
06:19:12 CTL "..."
06:19:37 PAG "..."
...


As you can see, it is a simple text file that contains a header record 
for the date, and then each line has a time along with various info.

What I would like to do is this:

1.  Compute the offset time (to adjust the erroneous timestamp to 
the correct time)
2.  Go through the file record by record.

3.  When you find a date header record, "*[YY-MM-DD=" grab the erroneous 
date (pos 12-19 as yy-mm-dd), but do NOT write it out.

4.  When you find a time record (hh:mm:ss in pos 1-8), put the bad 
date and time together and then subtract the offset time from it 
to get the right date and time.

5.  If the right date has changed from the prior record, write out 
the corrected date header record.

6.  write out the corrected time record (replacing the wrong time 
with the right time)

7.  Any other records other than a date header or time trailer, just 
write out as is.
So far this is what I have:

Rebol []
pr:	:probe
	ts-wrong:		29-jun-2003/23:51:00
	ts-right:		04-feb-2010/06:08:00
pr	ts-diff:		difference ts-right ts-wrong
pr	cutoff-12pm:	ts-wrong/time - ts-right/time
pr	time-offset:	24:00 - cutoff-12pm
	log-recs: read/lines %Madison-log-100204-blk.txt
	new-recs:	[]
	foreach rec log-recs [
		fields:		parse rec none
		log-rec:	load fields
		if time? log-rec/1 [print log-rec/1]
		append new-recs rec
		append new-recs newline
	]
	write %Madison-new-100204-blk.txt new-recs
I havent figured out LOAD or PARSE just yet, nor the other part of 
capturing / changing the dates and times
I'm open to any comments or ways to better handle this problem
I'm thinking LOAD is not something I should use.  It is choking on 
records that don't conform to rebol datatypes
Steeve
8-Mar-2010
[2918]
>> to-date form reverse parse copy/part at "*[YY-MM-DD=03-06-30]*" 
12 8 "-"
== 30-Jun-2003
PatrickP61
8-Mar-2010
[2919x2]
Steeve,  should the last "-" be a "=" instead?
to-date form parse copy/part at "*[YY-MM-DD=03-06-30]*" 12 8 none 
 This gives the same results as above but the year is wrong 2030 
intead of 2003.
BrianH
8-Mar-2010
[2921x2]
Break it down:
>> at "*[YY-MM-DD=03-06-30]*" 12
== "03-06-30]*"
>> copy/part at "*[YY-MM-DD=03-06-30]*" 12 8
== "03-06-30"
>> parse copy/part at "*[YY-MM-DD=03-06-30]*" 12 8 "-"
== ["03" "06" "30"]
>> reverse parse copy/part at "*[YY-MM-DD=03-06-30]*" 12 8 "-"
== ["30" "06" "03"]

>> form reverse parse copy/part at "*[YY-MM-DD=03-06-30]*" 12 8 "-"
== "30 06 03"

That's as far as I got on R3; I don't have R2 yet on this computer.
TO-DATE is more strict in R3. Let me see if I can figure out something 
that will work there too.
PatrickP61
8-Mar-2010
[2923]
Steve's formula is really close, but I need some way to put "-" inbetween 
the yy mm dd
BrianH
8-Mar-2010
[2924]
>> to-date map-each x reverse parse head insert copy/part at "*[YY-MM-DD=03-06-30]*" 
12 8 "20" "-" [to-integer x]
== 30-Jun-2003

>> to-date replace/all form reverse parse copy/part at "*[YY-MM-DD=03-06-30]*" 
12 8 "-" " " "-"
== 30-Jun-2003
PatrickP61
8-Mar-2010
[2925]
Two solutions  -- Thanks!
BrianH
8-Mar-2010
[2926x2]
Be sure to break them down as above so you know what they're doing.
And profile them to see which is better:


>> dp [to-date map-each x reverse parse head insert copy/part at 
"*[YY-MM-DD=03-06-30]*" 12 8 "20" "-" [to-integer x]]
== make object! [
    timer: 0:00:00.000023
    evals: 43
    eval-natives: 14
    eval-functions: 5
    series-made: 11
    series-freed: 0
    series-expanded: 0
    series-bytes: 731
    series-recycled: 0
    made-blocks: 6
    made-objects: 0
    recycles: 0
]


>> dp [to-date replace/all form reverse parse copy/part at "*[YY-MM-DD=03-06-30]*" 
12 8 "-" " " "-"]
== make object! [
    timer: 0:00:00.00004
    evals: 103
    eval-natives: 30
    eval-functions: 5
    series-made: 8
    series-freed: 0
    series-expanded: 0
    series-bytes: 530
    series-recycled: 0
    made-blocks: 2
    made-objects: 0
    recycles: 0
]
PatrickP61
8-Mar-2010
[2928]
BrianH or Steve,   I have seen some example code showing the following:

x:	copy []
y:	[]

These are both equal right,  Why do one over the other?
BrianH
8-Mar-2010
[2929]
They are not equal. The first makes a copy, the second references 
the original.
PatrickP61
8-Mar-2010
[2930]
but the original is empty block.
BrianH
8-Mar-2010
[2931x3]
Right, but it is the specific empty block. If x is modified it won't 
change the original, but if y is modified it will.
This matters more in code in a function. That function might get 
called again.
How to read the profiles above: The first is nearly twice the speed 
of the second, but creates more temporary memory.
PatrickP61
8-Mar-2010
[2934]
Ok, I get the if x is modified it won't change the original,

What I don't get is that and empty block [ ] is just empty.  It is 
not like a word or anything is it?

Yes, i did see the performance numbers.  that is good to see!
BrianH
8-Mar-2010
[2935x2]
The empty block is a value, even if it doesn't contain other values, 
and it is a value that can be modified.
I profile code patterns all the time, and when writing functions 
I use the best code patterns. This leads to better functions, even 
if you don't profile the whole function (which you can't always do).
PatrickP61
8-Mar-2010
[2937x2]
Ok just so I have this. 
x:	copy [ ]    will copy an empty block that x is refered to
y:	[ ] 	   will be a reference to an empty block


how is it possible to modify an empty block without referencing it?

 y: [ ]		<-- ref empty
== []

>> append "hi" y	<-- changes that empty block
== "hi"

>> x: [ ]			<-- X is now that same empty block
== []			But I don't see the "hi" value.  -- What am I missing?
OOOOOOHHHHHH
BrianH
8-Mar-2010
[2939]
You are missing reuse. It doesn't matter from the console, it matters 
in reused code, i.e. in functions.
PatrickP61
8-Mar-2010
[2940]
I have a typo in my example.  Now I see
BrianH
8-Mar-2010
[2941x4]
x is not the same empty block, it is a new block.
>> a: does [append copy [] 1]
>> b: does [append [] 1]
>> a
== [1]
>> a
== [1]
>> b
== [1]
>> b
== [1 1]
>> source a
a: make function! [[][append copy [] 1]]
>> source b
b: make function! [[][append [1 1] 1]]
This is in R3, but the rules are the same in R2.
PatrickP61
8-Mar-2010
[2945x6]
These examples are good to see
a: does>> a: does [append copy [ ] 1]
>> b: does [append [ ] 1]

>> c: does [append x: [ ] 1]         ;<--   is this the same as b?
Is C the same as B except that you can reference that memory pointer 
by using X -- is that right?
Ok so back to my question.

A will reference a specific memory and each time it is eval, a new 
empty block is setup.

B will reference a different memory place, and each time it is eval, 
that same memory can be modified.

C will reference a different memory place, that can also be modified 
by either using C or changing X.


But is there any significant difference to the following, if both 
reference a NEW memory location that is empty?
x:	copy [ ]
y:	[ ]

Sorry, I am really trying to understand
I think I understand now Brian,

In terms of just initializing a value, both of these are only done 
once, then they are essentially the same,

But if at any time you eval X or Y a second time, then you get different 
results!!!

Thank you for explaining it to me!
(after you have modified some values into x and Y, I mean)  -- hope 
I didn't confuse others out there.
Steeve
9-Mar-2010
[2951]
with pattern matching

>> attempt [to-date replace/all form reverse parse copy/part find/tail 
"*[YY-MM-DD=03-06-30]*" "YY-MM-DD="  8 "-" " " "-"]
== 30-Jun-2003
PatrickP61
10-Mar-2010
[2952]
I have a question about the APPEND function.

>> loop 10 [x: "a" append x "b"]
== "abbbbbbbbbb"


I would have expected the the final result to be just "ab" (after 
the 10th iteration).


But in this example, X has been assigned to the string "a" and then 
"b" is appended to it 10 times.

If X has been "reset" to the letter "a" again in each interation, 
why doesn't the previous "b" also go away since X has been reinitialized 
to just the letter "a"?
Henrik
10-Mar-2010
[2953x2]
you fell into the copy trap :-) x is not reset by assigning "a" to 
it.
x: copy "a" would solve that. that's why it's called the copy trap.
PatrickP61
10-Mar-2010
[2955]
You mean like this:

>> loop 10 [x: copy "a" append x "b"]
== "ab"
Henrik
10-Mar-2010
[2956x2]
with this, you can provide some interesting tricks to building series 
without assigning them to words:

loop 10 [append "a" "b"]
== "abbbbbbbbbb"
beware of this when making objects with series or just series that 
are copied. if you find that your series are changing in funny ways, 
it may be that you forgot to copy them.
Sunanda
10-Mar-2010
[2958]
Effectively, you are making x the _same_ as the string "a" so when 
one changes, they "both" do. as Henrik says, you want to initialise 
x to the _value_ of the string "a" instead.

It may be clearer like this
     A: copy "a"
    loop 10 [

        x: A    ;; x is the _same_ as A ... you want [x: copy A] to get its 
        value instead 
        append x "b"
    ]
Henrik
10-Mar-2010
[2959x2]
as a rule of thumb, REBOL tries to reuse series as much as it can.
use SAME? to detect whether two series are the same one:

>> a: ""
>> b: a
>> same? a b
== true

>> b: copy a
>> same? a b
== false
PatrickP61
10-Mar-2010
[2961]
>> loop 10 [x: "a" append x "b"]

And yet, if I repeat the exact same comand 10 times, I do NOT get 
the same result

x: "a" append x "b"
=="ab"
x: "a" append x "b"
=="ab"