Using numbers to access elements in arrays
Devin Asay
devin_asay at byu.edu
Thu Aug 7 10:55:01 EDT 2003
Correct me if I'm wrong...
It seems to me that Rev's implementation of arrays is not typical as
compared to lower level languages. It seems to be more of a hash table
(like in Java). In other words, you store data in arrays by associating
each piece of data with a named key, rather than placing the data in
enumerated elements. So when you put data into an array based on a
number, like this:
put "stuff" into myArray[1]
...you are essentially creating a key named "1", not placing the data
in the first element of the array.
This creates problems under certain conditions. Say I want to put data
into an array so that it is in a certain order. If I do it like this
(notice I do it out of order):
put "stuff" into myArray[1]
put "junk" into myArray[2]
put "nonsense" into myArray[4]
put "fluff" into myArray[5]
put "rubbish" into myArray[3]
... then I use a repeat loop to fetch it:
repeat with i = 1 to 5
put myArray[i] & space after aVar
end repeat
This works okay, yielding: stuff junk rubbish nonsense fluff
However, there are situations where this leads to problems;
specifically in using the rev database calls. I did something like this
(notice out of order again--this was necessitated by my script
structure):
put "stuff" into myArray[1]
put "junk" into myArray[2]
put "nonsense" into myArray[4]
put "fluff" into myArray[5]
put "rubbish" into myArray[3]
Then I wanted to use this array to update data in a mySQL table. So I
used the revExecuteSQL command like this (text wrapping alert):
revExecuteSQL connectionID,"update testlog set
quescorrect=quescorrect+:1, quesTotal=quesTotal+:2, t1_err=t1_err+:3,
t2_err=t2_err+:4, t3_err=t3_err+:5 where studID = 1","myArray"
What SEEMS to be happening--I'm still trying to pin it down--is that
the revExecuteSQL command is grabbing elements from the array in the
order in which they were created--traditional array
behavior--regardless of the key names (which happen to be numerals)
that I assigned to them earlier. So what ends up in the table is in
this order:
stuff junk nonsense fluff rubbish
Am I right here? Is rev assigning literal values "1", "2", "3", etc. as
key names, but the revExecuteSQL command is grabbing data from the
variable by counting elements in the order they were created? This is
causing me fits in one of my projects. If I can understand how it works
I'll know if I can use this approach, or if I need to retreat and try a
different approach to updating my database table.
Thanks.
Devin Asay
Humanities Technology and Research Support Center
Brigham Young University
More information about the use-livecode
mailing list