Unwanted Duplicate Data in Text Fields
[1/8] from: tserpa::earthlink::net at: 7-Nov-2003 11:38
In the code below, after I click "Load", if I immediately enter text in the
3rd text field, and then click "Forward" the text is not inserted into 'db.
However, if I click "Load", then immediately click "Forward" any number of
times, and then enter text into the 3rd or 4th text fields, the 3rd and 4th
fields of all records will immediately be set to the value entered. What is
happening?
The intended operation is to enter text into a field and have that value
saved in that field for that specific record.
%test.txt is just a csv file of the following format:
Joe,Red
Jim,Blue
John,Green
Jack,Black
REBOL []
view center-face layout [
f1: field 150x24
f2: field 150x24
f3: field 150x24 [probe db]
f4: field 150x24
button "Load" 150x24 [
data-in: read/lines %test.txt
db: copy []
record: copy []
foreach line data-in [
record: parse/all line ","
loop 2 [append record ""]
append/only db record
]
f1/text: db/1/1
f2/text: db/1/2
show [f1 f2]
]
across
button "Back" 71x24 [
db: back db
f1/text: db/1/1
f2/text: db/1/2
f3/text: db/1/3
f4/text: db/1/4
show [f1 f2 f3 f4]
]
button "Forward" 71x24 [
db-lgth: length? head db
db-idx: index? db
if db-idx < db-lgth [db: next db]
f1/text: db/1/1
f2/text: db/1/2
f3/text: db/1/3
f4/text: db/1/4
show [f1 f2 f3 f4]
]
]
[2/8] from: rotenca:telvia:it at: 7-Nov-2003 19:50
Hi Ted,
> In the code below, after I click "Load", if I immediately enter text in the
> 3rd text field, and then click "Forward" the text is not inserted into 'db.
> However, if I click "Load", then immediately click "Forward" any number of
> times, and then enter text into the 3rd or 4th text fields, the 3rd and 4th
> fields of all records will immediately be set to the value entered. What is
> happening?
The problem here is that after you call Forward the database and the field use
the SAME string (at the same memory location), so when you change the field
string you are also changing the database string.
But before you call Forward, some of the field string and the database strings
are different string (=stored in different memory locations).
When you Load the database, you assign the same string of the database only at
these 2 fields:
> f1/text: db/1/1
> f2/text: db/1/2
When you click Forward, you assign the same string of the database at all the
fields.
I do not think that sharing strings between fields and database is a good
idea, but i can be wrong.
---
Ciao
Romano
[3/8] from: tserpa:earthlink at: 7-Nov-2003 13:36
But why is the text that is entered into one text field automatically
propagated to all of the other empty fields in every record without
explicitly specifying it, i.e., after entering the text, I never insert the
value in the 3rd text field into db/1/3 and certainly not into db/1/4,
db/2/3, db/2/4, etc.?
How do I overcome this since I need to both display the values from the
database and manipulate them from the text field.
Ted
[4/8] from: greggirwin:mindspring at: 7-Nov-2003 13:29
Hi Ted,
TS> But why is the text that is entered into one text field automatically
TS> propagated to all of the other empty fields in every record without
TS> explicitly specifying it
Because of this line:
loop 2 [append record ""]
If you change it to
loop 2 [append record copy ""] ; << note the added COPY
you should get the expected result.
When you tell REBOL to use "" each time, it uses the *same* string for
all of them, not individual copies of an empty string. When you change
one, they all changed because they're all pointing to the same place
in memory where that one string lives, which they are all referring to.
-- Gregg
[5/8] from: Steven:White:ci:bloomington:mn:us at: 7-Nov-2003 14:37
Sorry if this is totally wrong, but does this have something to do with
the "PARA object"? I refer to the cookbook
www.rebol.net/cookbook/recipes/0029.html
I do not understand this AT ALL, yet. Do you have to assign data as
f1/text: copy db/1/1
?
Steven White
City of Bloomington
1800 W Old Shakopee Rd
Bloomington MN 55431-3096
USA
952-563-4882 (voice)
952-563-4672 (fax)
[steven--white--ci--bloomington--mn--us]
>>> [tserpa--earthlink--net] 11/07/03 01:36PM >>>
But why is the text that is entered into one text field automatically
propagated to all of the other empty fields in every record without
explicitly specifying it, i.e., after entering the text, I never insert
the
value in the 3rd text field into db/1/3 and certainly not into db/1/4,
db/2/3, db/2/4, etc.?
How do I overcome this since I need to both display the values from
the
database and manipulate them from the text field.
Ted
----- Original Message -----
From: "Romano Paolo Tenca" <[rotenca--telvia--it]>
To: <[rebol-list--rebol--com]>
Sent: Friday, November 07, 2003 12:50 PM
Subject: [REBOL] Re: Unwanted Duplicate Data in Text Fields
> Hi Ted,
>
> > In the code below, after I click "Load", if I immediately enter
text in
the
> > 3rd text field, and then click "Forward" the text is not inserted
into
'db.
> > However, if I click "Load", then immediately click "Forward" any
number
of
> > times, and then enter text into the 3rd or 4th text fields, the 3rd
and
4th
> > fields of all records will immediately be set to the value
entered.
What is
> > happening?
>
> The problem here is that after you call Forward the database and the
field
use
> the SAME string (at the same memory location), so when you change
the
field
> string you are also changing the database string.
>
> But before you call Forward, some of the field string and the
database
strings
> are different string (=stored in different memory locations).
>
> When you Load the database, you assign the same string of the
database
only at
> these 2 fields:
>
> > f1/text: db/1/1
> > f2/text: db/1/2
>
> When you click Forward, you assign the same string of the database
at all
the
[6/8] from: rotenca:telvia:it at: 7-Nov-2003 21:35
> But why is the text that is entered into one text field automatically
> propagated to all of the other empty fields in every record without
> explicitly specifying it, i.e., after entering the text, I never insert the
> value in the 3rd text field into db/1/3 and certainly not into db/1/4,
> db/2/3, db/2/4, etc.?
Because you are using the SAME string also for different database fields:
foreach line data-in [
record: parse/all line ","
loop 2 [append record ""] ;<------
append/only db record
]
"" is always the same string, you must use
copy ""
to create different strings.
---
Ciao
Romano
[7/8] from: SunandaDH:aol at: 7-Nov-2003 15:55
Ted:
> But why is the text that is entered into one text field automatically
> propagated to all of the other empty fields in every record without
> explicitly specifying it, i.e., after entering the text, I never insert the
> value in the 3rd text field into db/1/3 and certainly not into db/1/4,
> db/2/3, db/2/4, etc.?
It's a design feature of REBOL that trips everyone up at first because it is
so unlike most of the other languages we've ever used. I wrote off several
hours once to debugging the same problem.
Even local "variables" have this sort of "persistence" to them. Watch what
these strings do:
a-func: func [data /local var1 var2 var3][
var1: ""
var2: ""
var3: copy ""
append var1 data
append var2 data
append var3 data
print [var1 "--" var2 "--" var3]
]
loop 5 [a-func random/secure "ABCDE"]
One day, you'll thank Carl for this feature,
Sunanda.
[8/8] from: tserpa:earthlink at: 7-Nov-2003 15:11
Thanks to all that answered this question. The worst part is that I knew of
this pitfall but still failed to recognize it. Hopefully now I have learned
my lesson.
Thanks again,
Ted