How to retrieve a column array from a 2-D array?
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller
How to retrieve a column array from a 2-D array?
The manual doesn't give much info about arrays, so I have a question for you:
Consider a 2-dimensional array; the rows are records (1,2,3,....); the columns are info types ("name", "email", "phone", ...)
Now, tArray[1] will return an array containing the first record -- the value of each column for that record.
If I wanted to refer/retrieve just the emails of all the records as a sub-array of the larger array (i.e., a column array), is that possible?
In other words, is there a way to work with a whole column of a table of data?
Thanks,
Sri.
Consider a 2-dimensional array; the rows are records (1,2,3,....); the columns are info types ("name", "email", "phone", ...)
Now, tArray[1] will return an array containing the first record -- the value of each column for that record.
If I wanted to refer/retrieve just the emails of all the records as a sub-array of the larger array (i.e., a column array), is that possible?
In other words, is there a way to work with a whole column of a table of data?
Thanks,
Sri.
Re: How to retrieve a column array from a 2-D array?
Hi Sri,
Could you provide an example of the array you plane to build ?
Is it something like:
put "name" into tArray[1][1]
put "email" into tArray[2][1]
put "phone" into tArray[3][1]
?
Kind regards, Jean-Paul.
Could you provide an example of the array you plane to build ?
Is it something like:
put "name" into tArray[1][1]
put "email" into tArray[2][1]
put "phone" into tArray[3][1]
?
Kind regards, Jean-Paul.
Discovering LiveCode Community 6.5.2.
Re: How to retrieve a column array from a 2-D array?
Hi Jean-Paul:
I have over 1500 records ("rows") of people who have attended our workshops.
Each row has about 14 columns: last name, first name, email address, .........
I will need to be able to add new people, make collections of people who have registered for a particular workshop, etc.
I would often need to copy a column (copy the email column and paste it into an email, to send a bulk email, for instance).
Until recently, I was using Bento (made by FileMaker people). As you may be aware, they have recently discontinued Bento since last year. Now, the old Bento version is struggling to work with newer OSX versions, especially Mavericks. There are no adequate replacements in the market for Bento. I tried TapForms, but each minor change (such as changing the column display) takes over 5 minutes to update! It is not a professional product.
So, I decided to make myself a Bento replacement using LiveCode. That is the background story!
In this task, I find that the array manipulation capabilities of LiveCode are inadequate. I understand, of course, that LiveCode was not built for such purposes. (I am not a programmer in the traditional sense, but 20 years ago, I used to use Gauss, a matrix language for my academic research. That must have colored my expectation). I can process element by element, of course, but I think that would be very inefficient.
Thanks for listening,
Sri.
I have over 1500 records ("rows") of people who have attended our workshops.
Each row has about 14 columns: last name, first name, email address, .........
I will need to be able to add new people, make collections of people who have registered for a particular workshop, etc.
I would often need to copy a column (copy the email column and paste it into an email, to send a bulk email, for instance).
Until recently, I was using Bento (made by FileMaker people). As you may be aware, they have recently discontinued Bento since last year. Now, the old Bento version is struggling to work with newer OSX versions, especially Mavericks. There are no adequate replacements in the market for Bento. I tried TapForms, but each minor change (such as changing the column display) takes over 5 minutes to update! It is not a professional product.
So, I decided to make myself a Bento replacement using LiveCode. That is the background story!
In this task, I find that the array manipulation capabilities of LiveCode are inadequate. I understand, of course, that LiveCode was not built for such purposes. (I am not a programmer in the traditional sense, but 20 years ago, I used to use Gauss, a matrix language for my academic research. That must have colored my expectation). I can process element by element, of course, but I think that would be very inefficient.
Thanks for listening,
Sri.
-
- VIP Livecode Opensource Backer
- Posts: 9837
- Joined: Sat Apr 08, 2006 7:05 am
- Location: Los Angeles
- Contact:
Re: How to retrieve a column array from a 2-D array?
Associative arrays are useful for many things, but they are not intrinsically a database, and are not designed for easy querying across all elements.
With only 1500 records you may find it very easy to just use a tab-delimited list, or if you're thinking about future scalability you may consider storing the data in an SQLite database.
If simple delimited text will do the trick:
Then again, doing that with an array will also take only a millisecond or two:
With only 1500 records you may find it very easy to just use a tab-delimited list, or if you're thinking about future scalability you may consider storing the data in an SQLite database.
If simple delimited text will do the trick:
Code: Select all
function GetColumn pColNum, pData
set the itemdel to tab
repeat for each line tLine in pData
put item pColNum of tLine &cr after tList
end repeat
delete last char of tList
return tList
end GetColumn
Code: Select all
function GetArrayColumn pColName, pDataA
repeat for each element pElemA in pDataA
put pElemA[pColName] &cr after tList
end repeat
delete last char of tList
return tList
end GetArrayColumn
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
Re: How to retrieve a column array from a 2-D array?
Thanks, Richard.
Yes, I knew I could retrieve a column element by element; I was wondering if there was an in-built, faster way to reference a column directly.
I wondered if element-wise enumeration would be inefficient, but the dictionary does say that the use of the element keyword (in your example above) is "much faster" than manually iterating.
I assume this is not the case if I used the basic table.
Also, I should be able to manipulate the order of the columns in sub-collections (tables made out of selected rows and columns of the master data table). I see that I can do this by resetting the dgProp["columns"] property when I use an array. Not so simple with basic table, I guess.
Regards,
Sri.
Yes, I knew I could retrieve a column element by element; I was wondering if there was an in-built, faster way to reference a column directly.
I wondered if element-wise enumeration would be inefficient, but the dictionary does say that the use of the element keyword (in your example above) is "much faster" than manually iterating.
I assume this is not the case if I used the basic table.
Also, I should be able to manipulate the order of the columns in sub-collections (tables made out of selected rows and columns of the master data table). I see that I can do this by resetting the dgProp["columns"] property when I use an array. Not so simple with basic table, I guess.
Regards,
Sri.
Re: How to retrieve a column array from a 2-D array?
It may be a good idea for LiveCode to add a special form of Array named "Matrix," a two-dimensional array where the row keys are sequential numbers and the column keys can be unordered strings.
Add syntax to manipulate columns and rows, as well as basic spreadsheet-like operations where the data type permits.
I see a good cost-benefit ratio for such an addition.
Regards,
Sri.
Add syntax to manipulate columns and rows, as well as basic spreadsheet-like operations where the data type permits.
I see a good cost-benefit ratio for such an addition.
Regards,
Sri.
-
- VIP Livecode Opensource Backer
- Posts: 9837
- Joined: Sat Apr 08, 2006 7:05 am
- Location: Los Angeles
- Contact:
Re: How to retrieve a column array from a 2-D array?
How fast do you need it?sritcp wrote:I was wondering if there was an in-built, faster way to reference a column directly.
I don't understand; the manipulation of data in memory is a separate matter from its display in an object like a field.I wondered if element-wise enumeration would be inefficient, but the dictionary does say that the use of the element keyword (in your example above) is "much faster" than manually iterating.
I assume this is not the case if I used the basic table.
The DataGrid was written in LiveCode. Anything it does, you can do for other purposes however you need.Also, I should be able to manipulate the order of the columns in sub-collections (tables made out of selected rows and columns of the master data table). I see that I can do this by resetting the dgProp["columns"] property when I use an array. Not so simple with basic table, I guess.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
Re: How to retrieve a column array from a 2-D array?
Oh, I just meant that manipulating an array (using "for each element" as you have demonstrated) is probably much faster than manipulating a list (which underlies a Basic Table).FourthWorld wrote: I don't understand; the manipulation of data in memory is a separate matter from its display in an object like a field.
As for manipulation of columns, I was exploring if it would be possible to drag the columns to re-order them, as in Bento (not possible with DataGrid). Of course, I understand this is a UI issue, not a data manipulation issue.
Thanks,
Sri.
-
- VIP Livecode Opensource Backer
- Posts: 4002
- Joined: Sun Jan 07, 2007 9:12 pm
- Location: Bochum, Germany
Re: How to retrieve a column array from a 2-D array?
Hi Sri,
have a look at the attached stack. It is not Bento but a simple field that you fill with 2000 records and 4 columns. Then you switch column 1 and 4.
It indicates the time it takes.
If you want to go the Livecode way you will have to code. On the other hand there is still e.g FileMaker
Kind regards
Bernd
have a look at the attached stack. It is not Bento but a simple field that you fill with 2000 records and 4 columns. Then you switch column 1 and 4.
It indicates the time it takes.
If you want to go the Livecode way you will have to code. On the other hand there is still e.g FileMaker
Kind regards
Bernd
- Attachments
-
- 2000 records 4 columns.livecode.zip
- (1.48 KiB) Downloaded 221 times
Re: How to retrieve a column array from a 2-D array?
Hi Bernd:
Thanks, I'll take a look at your stack.
The problem is not only switching columns (that was an example) but many things: e.g., making a collection of a subset of records and columns, and still be linked to the master data -- so that, when I edit a collection, the master data is updated. I am trying to code these in LiveCode; let's see how it goes!
Bento and FileMaker are made by the same company. FileMaker too expensive (and feature-rich) for my needs.
Regards,
Sri.
Thanks, I'll take a look at your stack.
The problem is not only switching columns (that was an example) but many things: e.g., making a collection of a subset of records and columns, and still be linked to the master data -- so that, when I edit a collection, the master data is updated. I am trying to code these in LiveCode; let's see how it goes!
Bento and FileMaker are made by the same company. FileMaker too expensive (and feature-rich) for my needs.
Regards,
Sri.
-
- VIP Livecode Opensource Backer
- Posts: 7237
- Joined: Sat Apr 08, 2006 8:31 pm
- Location: Minneapolis MN
- Contact:
Re: How to retrieve a column array from a 2-D array?
Just a note that it's already possible to create an array with numbered keys. If you do not provide a secondary delimiter, that's what you get.sritcp wrote:It may be a good idea for LiveCode to add a special form of Array named "Matrix," a two-dimensional array where the row keys are sequential numbers and the column keys can be unordered strings.
Add syntax to manipulate columns and rows, as well as basic spreadsheet-like operations where the data type permits.
I see a good cost-benefit ratio for such an addition.
Regards,
Sri.
For the subject of this topic, see "combine" in the dictionary, particularly "combine by column", which could be helpful in extracting the column you want.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com
HyperActive Software | http://www.hyperactivesw.com
Re: How to retrieve a column array from a 2-D array?
Hi Jacque:
Thanks for the tip. However, I am having some trouble understanding what the dictionary says:
Combining an array by row converts the array into a table with rows separated by the rowDelimiter property. Each row in the resulting string is the contents of the corresponding key in the array.
Combining an array by column converts the array into a table with columns separated by the columnDelimiter property. Each column of the resulting string is the contents of the corresponding key in the array.
I tried the following code and it doesn't work. Where am I going wrong?
Thanks,
Sri
Thanks for the tip. However, I am having some trouble understanding what the dictionary says:
Combining an array by row converts the array into a table with rows separated by the rowDelimiter property. Each row in the resulting string is the contents of the corresponding key in the array.
Combining an array by column converts the array into a table with columns separated by the columnDelimiter property. Each column of the resulting string is the contents of the corresponding key in the array.
I tried the following code and it doesn't work. Where am I going wrong?
Code: Select all
on mouseUp
local a
put "blue" into a[1][1]
put "green" into a[2][1]
put "yellow" into a[3][1]
put "red" into a[4][1]
put "lemon" into a[1][2]
put "orange" into a[2][2]
put "peach" into a[3][2]
put "pineapple" into a[4][2]
combine a by row
put a into msg
end mouseUp
Sri
-
- VIP Livecode Opensource Backer
- Posts: 7237
- Joined: Sat Apr 08, 2006 8:31 pm
- Location: Minneapolis MN
- Contact:
Re: How to retrieve a column array from a 2-D array?
Say I create an array, for example:
gives a numbered array with the color names as the element contents. If you combine that by row, you get back a list of elements, basicalliy arranged in a column:
blue
green
yellow
red
lemon
orange
peach
pineapple
If instead you combine the array by column, you get a single row with the column data:
blue green yellow red lemon orange peach pineapple
The default delimiter is tab, but if I'd set it to comma the returned list would have been identical to the original.
Code: Select all
put "blue,green,yellow,red,lemon,orange,peach,pineapple" into tData
split tData by comma
blue
green
yellow
red
lemon
orange
peach
pineapple
If instead you combine the array by column, you get a single row with the column data:
blue green yellow red lemon orange peach pineapple
The default delimiter is tab, but if I'd set it to comma the returned list would have been identical to the original.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com
HyperActive Software | http://www.hyperactivesw.com
Re: How to retrieve a column array from a 2-D array?
Hi Jacque:
I see that this works with a one-dimensional array.
It doesn't seem to work with 2-D array, even if the keys are numerical (as my example in my previous post shows).
I think, in such cases, we can map a given 2-D array to another 2-D array with numeric keys and numeric data.
We can then use the 'transpose' function to extract one or more columns and then map back to the original array.
Regards,
Sri.
I see that this works with a one-dimensional array.
It doesn't seem to work with 2-D array, even if the keys are numerical (as my example in my previous post shows).
I think, in such cases, we can map a given 2-D array to another 2-D array with numeric keys and numeric data.
We can then use the 'transpose' function to extract one or more columns and then map back to the original array.
Regards,
Sri.
-
- VIP Livecode Opensource Backer
- Posts: 7237
- Joined: Sat Apr 08, 2006 8:31 pm
- Location: Minneapolis MN
- Contact:
Re: How to retrieve a column array from a 2-D array?
Oh right. Combine only works with a one dimensional array. So I guess you'll have to to try your second method.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com
HyperActive Software | http://www.hyperactivesw.com