Is it possible to set the keys of a loaded array?
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller
Is it possible to set the keys of a loaded array?
Let's say I've read some stored data from a file into a field; then, "split" the field by return and comma to form an array.
The resultant array has rows of records, each record has data that refer to "Date", "Time", "#Correct", "OutOf", and "Percent".
The array, though, would have been created with the default numeric keys of 1,2,3,... etc., for both rows and columns.
I'd like to leave the row keys as numeric, and set the column keys as ["theDate"], ["theTime"], etc.
I don't see (in the dictionary) a way to set the keys property; "keys" is not even a property.
I don't want to go the tedious route of using the "repeat" loop to load the array one cell at a time.
Anyway I can do this?
Thanks,
Sri.
The resultant array has rows of records, each record has data that refer to "Date", "Time", "#Correct", "OutOf", and "Percent".
The array, though, would have been created with the default numeric keys of 1,2,3,... etc., for both rows and columns.
I'd like to leave the row keys as numeric, and set the column keys as ["theDate"], ["theTime"], etc.
I don't see (in the dictionary) a way to set the keys property; "keys" is not even a property.
I don't want to go the tedious route of using the "repeat" loop to load the array one cell at a time.
Anyway I can do this?
Thanks,
Sri.
Re: Is it possible to set the keys of a loaded array?
Hi.
If you split data with two delimiters, the keys are the first element in each row. You only get default numeric keys when you specify a single delimiter.
Try it. Write back if either I am not getting what you mean, or you need more info.
Craig Newman
If you split data with two delimiters, the keys are the first element in each row. You only get default numeric keys when you specify a single delimiter.
Try it. Write back if either I am not getting what you mean, or you need more info.
Craig Newman
Re: Is it possible to set the keys of a loaded array?
Hi Craig:
I think I have the whole thing wrong. From what you say, if we read in a two column data and split it by two delimiters then the first column is always considered to be the keys. In that case, I can only read in a single column of data, am I right?
What if I have a data set of several rows (records) and five columns (representing "Date", "Time", "#Correct", "OutOf", and "Percent") -- something like this:
7/10/12 2:15 PM 7 10 70
7/11/12 3:30 PM 12 20 60
7/12/12 9:05 AM 6 15 40
.........
.........
What I was attempting to do was to read the above data into a field and then split it into an array. And, subsequently, assign them the column keys.
I see that this is not possible under LiveCode. I guess I have to read in the data into the array (from the file or a text field) one at a time, using the repeat loop. I felt that would be a tedious way to do it.
May be I am mistaken and there IS a more efficient way to do it in LiveCode?
Regards,
Sri.
I think I have the whole thing wrong. From what you say, if we read in a two column data and split it by two delimiters then the first column is always considered to be the keys. In that case, I can only read in a single column of data, am I right?
What if I have a data set of several rows (records) and five columns (representing "Date", "Time", "#Correct", "OutOf", and "Percent") -- something like this:
7/10/12 2:15 PM 7 10 70
7/11/12 3:30 PM 12 20 60
7/12/12 9:05 AM 6 15 40
.........
.........
What I was attempting to do was to read the above data into a field and then split it into an array. And, subsequently, assign them the column keys.
I see that this is not possible under LiveCode. I guess I have to read in the data into the array (from the file or a text field) one at a time, using the repeat loop. I felt that would be a tedious way to do it.
May be I am mistaken and there IS a more efficient way to do it in LiveCode?
Regards,
Sri.
Re: Is it possible to set the keys of a loaded array?
In the example, which column do you want the keys to be?
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com
HyperActive Software | http://www.hyperactivesw.com
Re: Is it possible to set the keys of a loaded array?
Jacque:
I am not sure I get your question.
I want the column names to be the keys ("Date", etc....).
So, if an array element is x[j],
i, which refers to the rows will be numeric keys, and
j, the columns will refer to the named keys.
My original question was, can I read just the data into a multidimensional array first (say 10 rows x 5 columns - as shown in my table in an earlier post),
and then assign the aforementioned keys later?
I am trying to see if there is a way of avoiding the tedious "repeat" loop":
repeat for each line i of the dataText
put element 1 into x["theDate"]
put element 2 into x["theTime"]
.......
end repeat
Regards,
Sri.
I am not sure I get your question.
I want the column names to be the keys ("Date", etc....).
So, if an array element is x[j],
i, which refers to the rows will be numeric keys, and
j, the columns will refer to the named keys.
My original question was, can I read just the data into a multidimensional array first (say 10 rows x 5 columns - as shown in my table in an earlier post),
and then assign the aforementioned keys later?
I am trying to see if there is a way of avoiding the tedious "repeat" loop":
repeat for each line i of the dataText
put element 1 into x["theDate"]
put element 2 into x["theTime"]
.......
end repeat
Regards,
Sri.
Re: Is it possible to set the keys of a loaded array?
That is correct. Split and combine haven't been updated to work with multidimensional arrays.I think I have the whole thing wrong. From what you say, if we read in a two column data and split it by two delimiters then the first column is always considered to be the keys. In that case, I can only read in a single column of data, am I right?
Re: Is it possible to set the keys of a loaded array?
Thanks, mwieder!
Decades ago, I used to work with a matrix manipulation language called Gauss, and that experience must have colored my thinking!
Regards,
Sri.
Decades ago, I used to work with a matrix manipulation language called Gauss, and that experience must have colored my thinking!
Regards,
Sri.
Re: Is it possible to set the keys of a loaded array?
I think you'll find that it actually isn't all that "tedious" and ridiculously fast as well...
Code: Select all
put "theDate,theTime,etc" into tHeaders
put 1 into tLineCount
set the itemDelimiter to comma
repeat for each line tLine of theDataText
put 1 into i
repeat for each item tElement in tLine
put tElement into x[tLineCount][item i of tHeaders]
add 1 to i
end repeat
add 1 to tLineCount
end repeat
Re: Is it possible to set the keys of a loaded array?
Hi mwieder:
You are probably right that the long route works fast enough. I just think it would be much more efficient and flexible if we could read a set of numbers in to an array and assign keys (i.e., row and column labels) independently.
Thanks for the code. I see the error in my code; the iterating variable represents the line itself, not the line number.
Thanks,
Sri.
You are probably right that the long route works fast enough. I just think it would be much more efficient and flexible if we could read a set of numbers in to an array and assign keys (i.e., row and column labels) independently.
Thanks for the code. I see the error in my code; the iterating variable represents the line itself, not the line number.
Thanks,
Sri.
Re: Is it possible to set the keys of a loaded array?
Sorry, I couldn't let go of this problem. I think I've found a way to load an entire two-dimensional array of data in one go.
1. Create a data grid table (make it invisible, if necessary).
2. Set its dgText to a file containing the data, with the first line listing the headers ("column keys").
3. Now, put the dgData of the data grid table into an array.
That's it! I am amazed it works!
I don't know if this is faster or more efficient (under the hood). It sure seems simpler and more elegant.
Regards,
Sri.
1. Create a data grid table (make it invisible, if necessary).
2. Set its dgText to a file containing the data, with the first line listing the headers ("column keys").
3. Now, put the dgData of the data grid table into an array.
That's it! I am amazed it works!
I don't know if this is faster or more efficient (under the hood). It sure seems simpler and more elegant.
Regards,
Sri.
Re: Is it possible to set the keys of a loaded array?
Hi.
Read up on the "dgData". It saves a step.
Craig Newman
Read up on the "dgData". It saves a step.
Craig Newman
Re: Is it possible to set the keys of a loaded array?
Hi Craig:
The problem I was trying to solve was to get a datafile read into a multidimensional array without having to read one line at a time using the repeat loop.
mwieder seemed to suggest that it may not be possible to do that in LC; you had to loop it in, line by line.
It seemed to me that the two-step
dataFile --> dgText; dgData --> myArray
accomplished it.
I tried to read up on "dgData"; I couldn't see a way to move a file into dgData without going through dgText (or reading it line by line).
Of course, I could be wrong; I am very new at this.
Regards,
Sri.
The problem I was trying to solve was to get a datafile read into a multidimensional array without having to read one line at a time using the repeat loop.
mwieder seemed to suggest that it may not be possible to do that in LC; you had to loop it in, line by line.
It seemed to me that the two-step
dataFile --> dgText; dgData --> myArray
accomplished it.
I tried to read up on "dgData"; I couldn't see a way to move a file into dgData without going through dgText (or reading it line by line).
Of course, I could be wrong; I am very new at this.
Regards,
Sri.
Re: Is it possible to set the keys of a loaded array?
The datagrid is a LiveCode scripted library, so it's doing the repeat for you. I think you could accomplish the same thing with less overhead by popping the contents of your file into a variable and running the loop on it, but either method is fast enough and the convenience may be worth it.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com
HyperActive Software | http://www.hyperactivesw.com
Re: Is it possible to set the keys of a loaded array?
I suspected as much!jacque wrote:The datagrid is a LiveCode scripted library, so it's doing the repeat for you.....
Thanks,
Sri.