Is it possible to set the keys of a loaded array?

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
sritcp
Posts: 431
Joined: Tue Jun 05, 2012 5:38 pm

Is it possible to set the keys of a loaded array?

Post by sritcp » Wed Jul 18, 2012 8:07 pm

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.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10321
Joined: Wed May 06, 2009 2:28 pm

Re: Is it possible to set the keys of a loaded array?

Post by dunbarx » Thu Jul 19, 2012 1:19 am

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

sritcp
Posts: 431
Joined: Tue Jun 05, 2012 5:38 pm

Re: Is it possible to set the keys of a loaded array?

Post by sritcp » Thu Jul 19, 2012 6:48 pm

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.

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7393
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: Is it possible to set the keys of a loaded array?

Post by jacque » Thu Jul 19, 2012 7:31 pm

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

sritcp
Posts: 431
Joined: Tue Jun 05, 2012 5:38 pm

Re: Is it possible to set the keys of a loaded array?

Post by sritcp » Thu Jul 19, 2012 9:24 pm

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.

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Contact:

Re: Is it possible to set the keys of a loaded array?

Post by mwieder » Thu Jul 19, 2012 9:52 pm

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?
That is correct. Split and combine haven't been updated to work with multidimensional arrays.

sritcp
Posts: 431
Joined: Tue Jun 05, 2012 5:38 pm

Re: Is it possible to set the keys of a loaded array?

Post by sritcp » Thu Jul 19, 2012 11:09 pm

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.

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Contact:

Re: Is it possible to set the keys of a loaded array?

Post by mwieder » Thu Jul 19, 2012 11:46 pm

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

sritcp
Posts: 431
Joined: Tue Jun 05, 2012 5:38 pm

Re: Is it possible to set the keys of a loaded array?

Post by sritcp » Fri Jul 20, 2012 12:59 am

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.

sritcp
Posts: 431
Joined: Tue Jun 05, 2012 5:38 pm

Re: Is it possible to set the keys of a loaded array?

Post by sritcp » Fri Jul 20, 2012 6:59 pm

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.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10321
Joined: Wed May 06, 2009 2:28 pm

Re: Is it possible to set the keys of a loaded array?

Post by dunbarx » Sat Jul 21, 2012 2:05 am

Hi.

Read up on the "dgData". It saves a step.

Craig Newman

sritcp
Posts: 431
Joined: Tue Jun 05, 2012 5:38 pm

Re: Is it possible to set the keys of a loaded array?

Post by sritcp » Sat Jul 21, 2012 3:57 pm

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.

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7393
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: Is it possible to set the keys of a loaded array?

Post by jacque » Sat Jul 21, 2012 6:35 pm

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

sritcp
Posts: 431
Joined: Tue Jun 05, 2012 5:38 pm

Re: Is it possible to set the keys of a loaded array?

Post by sritcp » Sun Jul 22, 2012 6:50 pm

jacque wrote:The datagrid is a LiveCode scripted library, so it's doing the repeat for you.....
I suspected as much!

Thanks,
Sri.

Post Reply