Page 1 of 1
How to retrieve a ROW from a 2D array
Posted: Mon Feb 28, 2022 10:58 pm
by laouris
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.
Re: How to retrieve a ROW from a 2D array
Posted: Mon Feb 28, 2022 11:02 pm
by dunbarx
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:
1,dog,friend
2,cat,foe
3,bird,nuisance
4,duck,dinner
In plain English, assuming we had this in some form of array, what do you want out of, say, row 2? Is it:
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
Re: How to retrieve a ROW from a 2D array
Posted: Mon Feb 28, 2022 11:54 pm
by dunbarx
Aha. Maybe this is what you are asking? If you have this:
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
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
Re: How to retrieve a ROW from a 2D array
Posted: Tue Mar 01, 2022 12:21 am
by stam
laouris wrote: ↑Mon Feb 28, 2022 10:58 pm
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.
Hi laouris, I'm assuming the array described above has been constructed in LC in he form of
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
however keep in mind that arrays in LC are associative, not numerically indexed - there is no inherent order. You can sort by key if you want to find the lowest ["ID"] for example and get the key from there.
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
Re: How to retrieve a ROW from a 2D array
Posted: Tue Mar 01, 2022 4:03 am
by FourthWorld
Sounds like you don't need an array. Maybe a simple delimited string will do.
Re: How to retrieve a ROW from a 2D array
Posted: Tue Mar 01, 2022 4:33 am
by dunbarx
IBetter, a 2-D table.
Then you have a frood who really knows where his rows are
Craig
Re: How to retrieve a ROW from a 2D array
Posted: Tue Mar 01, 2022 4:36 am
by dunbarx
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
Re: How to retrieve a ROW from a 2D array
Posted: Tue Mar 01, 2022 8:54 am
by laouris
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"
Re: How to retrieve a ROW from a 2D array
Posted: Tue Mar 01, 2022 2:52 pm
by dunbarx
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:
Code: Select all
put the dgText of group "yourDGHere" into temp
answer line 2 of temp
Craig
EDIT. Or even shorter:
Code: Select all
on mouseup
answer line 2 of the dgText of grp "yourDG"
end mouseup
Re: How to retrieve a ROW from a 2D array
Posted: Tue Mar 01, 2022 2:52 pm
by stam
laouris wrote: ↑Tue Mar 01, 2022 8:54 am
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"
Hi laouris,
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"
Re: How to retrieve a ROW from a 2D array
Posted: Tue Mar 01, 2022 3:01 pm
by dunbarx
Since you are already in a dataGrid, try this is a button script on your posted stack:
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
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
Re: How to retrieve a ROW from a 2D array
Posted: Tue Mar 01, 2022 3:10 pm
by stam
Also:
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
to show the selected line (rather than a hardcoded number) in the field