Func, why does it remeber previous data?
[1/7] from: micael:gullmes:telenordia:se at: 10-Nov-2000 22:10
Hi all,
I am playing around with a function that search through a block containing
binary values.
My function searches through the block for the binary #{ACDC} plus #{??}
The problem I encounter is when I run the function multiple times, the
binary variable "data_to_find" remembers old data from the same function,
even if I clear it.
Can someone please explain this behaviour and give me some hints of how to
get around it?
Here's the script:
-----------------------------------------------------------
find_stuff: func ["Find stuff in data_block"] [
tail_data: ""
tail_data: ask "what data do you want to find after ACDC? "
probe data_to_find: ""
probe clear data_to_find
probe data_to_find: #{ACDC}
probe append data_to_find do rejoin ["#{" tail_data "}"]
result: []
clear result
foreach data data_block [
if find data data_to_find [
append result data
]
]
print rejoin ["Found " length? result " containing " data_to_find]
]
------------------------------------------------------------
Here's an example of running the script:
------------------------------------------------------------
>> data_block: [#{ACDC01} #{ACDC02} #{ACDC03} #{ACDC01} #{ACDC02}]
== [#{ACDC01} #{ACDC02} #{ACDC03} #{ACDC01} #{ACDC02}]
>> find_stuff
what data do you want to find after ACDC? 01
#{ACDC}
#{ACDC01}
Found 2 containing #{ACDC01}
>> find_stuff
what data do you want to find after ACDC? 01
#{ACDC01} <----- Where does 01 come from?
#{ACDC0101}
Found 0 containing #{ACDC0101}
>> find_stuff
what data do you want to find after ACDC? 02
#{ACDC0101} <----- Where does 0101 come from?
#{ACDC010102}
Found 0 containing #{ACDC010102}
>>
-----------------------------------------------------------
Brgds /Micael
[2/7] from: al:bri:xtra at: 11-Nov-2000 11:41
> tail_data: ""
Use:
tail_data: make string! 0
instead.
Think of ":" as being:
Set the word to the left to _POINT_ to the string at the right.
So this:
tail_data: ""
actually means:
Set the word "tail_data" to this particular empty string. It's like
assigning a pointer in C/C++, OK?
The same thing applies here:
> result: []
Use:
result: make block! 0
instead.
Andrew Martin
ICQ: 26227169
http://members.nbci.com/AndrewMartin/
[3/7] from: rebol::techscribe::com at: 10-Nov-2000 15:05
Hi Micael,
you wrote:
> probe data_to_find: #{ACDC}
> probe append data_to_find do rejoin ["#{" tail_data "}"]
1. You associate the word data_to_find with the binary literal #{ACDC}. Now
any manipulation that is directed at data_to_find will modify the string
${ACDC}. Specifically when you
2. append the result of the expression do rejoin [...] to data_to_find, that
result is appended to the binary literal #{ACDC}. When the function is called
again at a later point in time data_to_find is assigned to the binary liter
#{ACDC <....>} where <...> is a placeholder for whatever it is you appended
to the literal string on previous function calls.
3. Setting data_to_find to an empty string literal before you set
data_to_find to the binary literal, and/or clearing that string literal does
not (and cannot) in any way effect the binary literal. First the word pointed
at an empty string literal that was cleared, now it points at a different
literal value, the binary literal that has accumulated data that was appended
to it during the previous function call.
4. Use data_to_find: copy #{ACDC} or data_to_find: make binary! {ACDC} to
prevent the appended data from accumulating in the binary literal that you
want preserved as is. The copy of the binary literal will be affected by
subsequent manipulations (append ...), whereas the prototype binary literl
#{ACDC} remains unmodified.
If you program in C then think of data_to_find as a pointer to a memory
address. Whatever you do to data_to_find directly effects the value it is
associated with, either the prototype string literal itself, or the unique
copy of it that you force using copy or make binary!.
Hope this helps,
Elan
[4/7] from: micael:gullmes:telenordia:se at: 11-Nov-2000 0:06
Hej Andrew,
Thanks for the hints, I changed according to your suggestions.
But the function still gets incorrect data, any other ideas?
---
find_stuff2: func ["Find stuff in data_block"] [
tail_data: make string! 0
tail_data: ask "what data do you want to find after ACDC? "
probe data_to_find: make string! 0
probe clear data_to_find
probe data_to_find: #{ACDC}
probe append data_to_find do rejoin ["#{" tail_data "}"]
result: make block! 0
clear result
foreach data data_block [
if find data data_to_find [
append result data
]
]
print rejoin ["Found " length? result " containing " data_to_find]
]
But I still get the same fault:
>> find_stuff2
what data do you want to find after ACDC? 01
#{ACDC}
#{ACDC01}
Found 2 containing #{ACDC01}
>> find_stuff2
what data do you want to find after ACDC? 01
#{ACDC01}
#{ACDC0101}
Found 0 containing #{ACDC0101}
Brgds /Micael
-----Ursprungligt meddelande-----
Från: Andrew Martin [mailto:[Al--Bri--xtra--co--nz]]
Skickat: den 10 november 2000 23:41
Till: [rebol-list--rebol--com]
Ämne: [REBOL] Re: Func, why does it remeber previous data?
> tail_data: ""
Use:
tail_data: make string! 0
instead.
Think of ":" as being:
Set the word to the left to _POINT_ to the string at the right.
So this:
tail_data: ""
actually means:
Set the word "tail_data" to this particular empty string. It's like
assigning a pointer in C/C++, OK?
The same thing applies here:
> result: []
Use:
result: make block! 0
instead.
Andrew Martin
ICQ: 26227169
http://members.nbci.com/AndrewMartin/
[5/7] from: micael:gullmes:telenordia:se at: 11-Nov-2000 0:17
Elan,
Thanks, now I understand, and it works like a charm.
Brgds/Micael
-----Ursprungligt meddelande-----
Fran: Elan [mailto:[rebol--techscribe--com]]
Skickat: den 11 november 2000 00:05
Till: [rebol-list--rebol--com]
Amne: [REBOL] Re: Func, why does it remeber previous data?
Hi Micael,
you wrote:
> probe data_to_find: #{ACDC}
> probe append data_to_find do rejoin ["#{" tail_data "}"]
1. You associate the word data_to_find with the binary literal #{ACDC}. Now
any manipulation that is directed at data_to_find will modify the string
${ACDC}. Specifically when you
2. append the result of the expression do rejoin [...] to data_to_find, that
result is appended to the binary literal #{ACDC}. When the function is
called
again at a later point in time data_to_find is assigned to the binary liter
#{ACDC <....>} where <...> is a placeholder for whatever it is you appended
to the literal string on previous function calls.
3. Setting data_to_find to an empty string literal before you set
data_to_find to the binary literal, and/or clearing that string literal does
not (and cannot) in any way effect the binary literal. First the word
pointed
at an empty string literal that was cleared, now it points at a different
literal value, the binary literal that has accumulated data that was
appended
to it during the previous function call.
4. Use data_to_find: copy #{ACDC} or data_to_find: make binary! {ACDC} to
prevent the appended data from accumulating in the binary literal that you
want preserved as is. The copy of the binary literal will be affected by
subsequent manipulations (append ...), whereas the prototype binary literl
#{ACDC} remains unmodified.
If you program in C then think of data_to_find as a pointer to a memory
address. Whatever you do to data_to_find directly effects the value it is
associated with, either the prototype string literal itself, or the unique
copy of it that you force using copy or make binary!.
Hope this helps,
Elan
[6/7] from: chris:starforge at: 10-Nov-2000 21:35
#10-Nov-00# Message from [*Micael--Gullmes--telenordia--se*]:
Hi [Micael--Gullmes--telenordia--se],
> Can someone please explain this behaviour and give me some hints of how to
> get around it?
I hit the same problem in my reboldoc.r script, I resported to converting
all the
append foo bar
with
foo: rejoin [foo bar]
because of it. I tried everything - clearing, allocating strings explicitly,
banging my head against a brick wall, all the usual stuff - nothing stopped
it :/
Chris
--
New sig in the works
Explorer 2260, Designer and Coder
http://www.starforge.co.uk
--
Death is nature's way of telling you to slow down
[7/7] from: emptyhead:home:nl at: 11-Nov-2000 13:49
You forgot to copy the data_to_find binary value. Replace the
probe data_to_find: #{ACDC}
with data_to_find: copy #{ACDC}
The append changes the series!... an example:
>> a: [ b: #{ACDC} append b #{a0} ]
== [b: #{ACDC} append b #{A0}]
>> do a
== #{ACDCA0}
>> a
== [b: #{ACDCA0} append b #{A0}]
>>
If you do not copy the series your will change your own code.
>> a: [ b: #{ACDC} append copy b #{a0} ]
== [b: #{ACDC} append copy b #{A0}]
>> do a
== #{ACDCA0}
>> a
== [b: #{ACDC} append copy b #{A0}]
>>
That should fix it...
Daan Oosterveld
[Micael--Gullmes--telenordia--se] schreef: