How to retrieve a ROW from a 2D array
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller
How to retrieve a ROW from a 2D array
Assume an 2D array:
ID Group Label State
1 3 Financial 1
2 5 Other 1
3 6 Political 0
4 7 Civil 0
Question 1: How can I retrieve the data of a single Row?
Question 2: How can I get the Group element which is the first whose state is zero (in this case 6)?
Your help is truly appreciated. I have spent many hours on this simple task but I do not seem to understand how 2D arrays in Livecode work.
ID Group Label State
1 3 Financial 1
2 5 Other 1
3 6 Political 0
4 7 Civil 0
Question 1: How can I retrieve the data of a single Row?
Question 2: How can I get the Group element which is the first whose state is zero (in this case 6)?
Your help is truly appreciated. I have spent many hours on this simple task but I do not seem to understand how 2D arrays in Livecode work.
-
- VIP Livecode Opensource Backer
- Posts: 9647
- Joined: Wed May 06, 2009 2:28 pm
- Location: New York, NY
Re: How to retrieve a ROW from a 2D array
Hi.
I started to answer you in the old thread that you posted today. Anyway,
Hi.
Arrays in LC are not really built the way you are asking. That is, if I actually understand what you are asking. So let's say you have the following dataSet:
And therefore would "row" 3 be:
My real point is that arrays can be constructed in several ways, and deconstructed in any way you wish, but it is likely that such deconstruction will require that you process the data in the array using "straight" LiveCode, that is, you extract the data back "into the clear" with the "combine" command, and then use all of LC's powerful text and chunking gadgetry to massage your data into whatever form that you need. In other words, array variables do not much lend themselves to that sort of massaging directly. They are rather insular in character, and their contents have to be converted into "normal" variables in order to be worked on.
Craig
I started to answer you in the old thread that you posted today. Anyway,
Hi.
Arrays in LC are not really built the way you are asking. That is, if I actually understand what you are asking. So let's say you have the following dataSet:
In plain English, assuming we had this in some form of array, what do you want out of, say, row 2? Is it:1,dog,friend
2,cat,foe
3,bird,nuisance
4,duck,dinner
??dog,cat,bird,duck
And therefore would "row" 3 be:
??friend.foe,nuisance,dinner
My real point is that arrays can be constructed in several ways, and deconstructed in any way you wish, but it is likely that such deconstruction will require that you process the data in the array using "straight" LiveCode, that is, you extract the data back "into the clear" with the "combine" command, and then use all of LC's powerful text and chunking gadgetry to massage your data into whatever form that you need. In other words, array variables do not much lend themselves to that sort of massaging directly. They are rather insular in character, and their contents have to be converted into "normal" variables in order to be worked on.
Craig
-
- VIP Livecode Opensource Backer
- Posts: 9647
- Joined: Wed May 06, 2009 2:28 pm
- Location: New York, NY
Re: How to retrieve a ROW from a 2D array
Aha. Maybe this is what you are asking? If you have this:
You see that you get "foe" in the first answer dialog. But empty in the second , where you might have expected "cat". And of course you get empty in the third, where you might have expected your "row" to appear. This is the way LC arrays are structured. You cannot "pick out" an intermediate value, because those iare merely the keys for the following child element. There are many discussions about this. I will see if I can locate one.
Craig
Code: Select all
on mouseup
put "friend" into animalArray [1]["dog"]
put "foe" into animalArray [2]["cat"]
put "nuisance" into animalArray [3]["bird"]
put "dinner" into animalArray [4]["duck"]
answer animalArray[2]["cat"]
answer animalArray[2]
repeat with y = 1 to the number of lines of the keys of animalArray
put animalArray[y] & comma after temp
end repeat
answer temp
end mouseup
Craig
Re: How to retrieve a ROW from a 2D array
Hi laouris, I'm assuming the array described above has been constructed in LC in he form oflaouris wrote: ↑Mon Feb 28, 2022 10:58 pmAssume an 2D array:
ID Group Label State
1 3 Financial 1
2 5 Other 1
3 6 Political 0
4 7 Civil 0
Question 1: How can I retrieve the data of a single Row?
Question 2: How can I get the Group element which is the first whose state is zero (in this case 6)?
Your help is truly appreciated. I have spent many hours on this simple task but I do not seem to understand how 2D arrays in Livecode work.
2DArray["ID"]["Group"]
2DArray["ID"]["Label"]
2DArray["ID"]["State"]
to represent the columns above, as arrays in LC are not numerically ordered.
You can easily filter an array into a new array for any key:
Code: Select all
filter elements of 2DArray where each["State"] = 0 into 2DNewArray
you can sort and return an full array sorted by a specified key, or return just the sorted list of key indexes - there is a massive thread on sorting here: viewtopic.php?f=7&t=28678
after sorting you can get the sorted list of keys as return-delimited list and get the first line
Hope that helps or at least makes sense,
Stam
-
- VIP Livecode Opensource Backer
- Posts: 9823
- Joined: Sat Apr 08, 2006 7:05 am
- Location: Los Angeles
- Contact:
Re: How to retrieve a ROW from a 2D array
Sounds like you don't need an array. Maybe a simple delimited string will do.
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
-
- VIP Livecode Opensource Backer
- Posts: 9647
- Joined: Wed May 06, 2009 2:28 pm
- Location: New York, NY
Re: How to retrieve a ROW from a 2D array
IBetter, a 2-D table.
Then you have a frood who really knows where his rows are
Craig
Then you have a frood who really knows where his rows are
Craig
-
- VIP Livecode Opensource Backer
- Posts: 9647
- Joined: Wed May 06, 2009 2:28 pm
- Location: New York, NY
Re: How to retrieve a ROW from a 2D array
I think there is a tendency, especially among newer users, to use the coolest gadgets early in their careers.
Like arrays and dataGrdis, when tables and table fields are much simpler and more accessible, and do much the same job.
Craig
Like arrays and dataGrdis, when tables and table fields are much simpler and more accessible, and do much the same job.
Craig
Re: How to retrieve a ROW from a 2D array
Hi Guys. I applied some of the advice above but I cannot get it to work. I attach a very simple program to show you that the combine does not seem to be working as expected. What i would really appreciate is FIXING it and sharing it....
Simply click Clear to empty the Grid
Click "Create 2 Array" to create and display in the Grid
Click "Filter by State" to extract only 2 last lines of the array
And then let me know why the simple command for combining the array to table and displaying in the field does not seem to work
combine tSortedArray by row OR combine tSortedArray using return and comma
put tSortedArray into field "f_Table"
Simply click Clear to empty the Grid
Click "Create 2 Array" to create and display in the Grid
Click "Filter by State" to extract only 2 last lines of the array
And then let me know why the simple command for combining the array to table and displaying in the field does not seem to work
combine tSortedArray by row OR combine tSortedArray using return and comma
put tSortedArray into field "f_Table"
- Attachments
-
- Extract Col_Row_2D ArraySIMPLE.livecode.zip
- (6.61 KiB) Downloaded 69 times
-
- VIP Livecode Opensource Backer
- Posts: 9647
- Joined: Wed May 06, 2009 2:28 pm
- Location: New York, NY
Re: How to retrieve a ROW from a 2D array
Hmmm.
I only just now see that you are dealing with a dataGrid. All this talk about the structure of arrays aside, why not just:
Craig
EDIT. Or even shorter:
I only just now see that you are dealing with a dataGrid. All this talk about the structure of arrays aside, why not just:
Code: Select all
put the dgText of group "yourDGHere" into temp
answer line 2 of temp
EDIT. Or even shorter:
Code: Select all
on mouseup
answer line 2 of the dgText of grp "yourDG"
end mouseup
Last edited by dunbarx on Tue Mar 01, 2022 4:31 pm, edited 3 times in total.
Re: How to retrieve a ROW from a 2D array
Hi laouris,laouris wrote: ↑Tue Mar 01, 2022 8:54 amHi Guys. I applied some of the advice above but I cannot get it to work. I attach a very simple program to show you that the combine does not seem to be working as expected. What i would really appreciate is FIXING it and sharing it....
Simply click Clear to empty the Grid
Click "Create 2 Array" to create and display in the Grid
Click "Filter by State" to extract only 2 last lines of the array
And then let me know why the simple command for combining the array to table and displaying in the field does not seem to work
combine tSortedArray by row OR combine tSortedArray using return and comma
put tSortedArray into field "f_Table"
the combine doesn't work because the dgData is multidimensional (i think! - others may correct me).
You can achieve what you want however using the dgText of the data grid. For example, to extract the first row:
Code: Select all
put line 1 of the dgText of group "clustersDataGrid" into field "f_row"
-
- VIP Livecode Opensource Backer
- Posts: 9647
- Joined: Wed May 06, 2009 2:28 pm
- Location: New York, NY
Re: How to retrieve a ROW from a 2D array
Since you are already in a dataGrid, try this is a button script on your posted stack:
Voila, (a la Klaus) you have row 2.
EDIT. See my earlier post about two up. Anyway, fooling around with arrays is very useful in its own right. But you now have a few practical ways to actually do the job you needed, because a dataGrid has built-in tools for just your purpose.
Craig
Code: Select all
on mouseup
get the dgDataOfLine[2] of grp 1
combine it with return and comma
repeat with y = 1 to the number of lines of it
put item 2 of line y of it & comma after temp
end repeat
answer temp
end mouseup
EDIT. See my earlier post about two up. Anyway, fooling around with arrays is very useful in its own right. But you now have a few practical ways to actually do the job you needed, because a dataGrid has built-in tools for just your purpose.
Craig
Last edited by dunbarx on Tue Mar 01, 2022 4:30 pm, edited 1 time in total.
Re: How to retrieve a ROW from a 2D array
Also:
to show the selected line (rather than a hardcoded number) in the field
Code: Select all
on mouseUp
put line (the dgHilitedLine of group "ClustersDataGrid") of the dgText of group "ClustersDataGrid" into field "f_row"
replace tab with ", " in field "f_row"
end mouseUp