Creating/Populating Associative Array[Solved]

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
antrax13
Posts: 39
Joined: Mon Jun 01, 2015 11:38 am

Creating/Populating Associative Array[Solved]

Post by antrax13 » Wed Aug 26, 2015 1:58 pm

Hi All,
I ve been trying for a while with no success to create associative array
When I am looping via data grid I am getting these values => id and description for each loop
I will get this information for every single loop

Code: Select all

9 ----------- Description Test
1 ----------- Description Test Test
3 ----------- Description Test Test Test
so my code for this is

Code: Select all

repeat for each element e in AvailableArray
   answer e["id"]&"--------------"&e["description"]
 -- HERE needs to be a code to create bottom array
end repeat 
but I am not able to construct array that looks exactly like the one I am showing below

Code: Select all

Array
..	[1] => Array
..	.	[id] => 9
..	.	[description] => Description Test
..	[2] => Array
..	.	[id] => 1
..	.	[description] => Description Test Test
..	[3] => Array
..	.	[id] => 3
..	.	[description] => Description Test Test Test
Reason why I need this is that I have data grid where I can select rows and when I select these rows I need to populate another grid with selected rows.

Now Imagine I will select row with ID 9 and 3 I need to create an array that will look like this

Code: Select all

Array
..	[1] => Array
..	.	[id] => 9
..	.	[description] => Description Test
..	[2] => Array
..	.	[id] => 3
..	.	[description] => Description Test Test Test
Last edited by antrax13 on Wed Aug 26, 2015 2:15 pm, edited 1 time in total.

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

Re: Creating/Populating Associative Array

Post by dunbarx » Wed Aug 26, 2015 2:07 pm

Hi.

Make a button and a field. In the button script:

Code: Select all

on mouseup
   put "" into fld 1
   repeat with y = 1 to 10
      put 2 * y into tArray[y}
   end repeat
 answer the keys of tArray
   
   set itemdel to "-"
   combine tArray with return and "-"
   sort tArray numeric by item 2 of each
   put tArray into  fld 1
end mouseup
This gives a few numbers and their doubles.

Now this in the button script:

Code: Select all

on mouseUp
   put "" into fld 1
   repeat 100
      put any word of "zz aa bb ss dd" & space after temp
   end repeat
   put temp into line 1 of fld 1
   repeat for each word tWord in temp
      add 1 to tWordCount[tWord] --ADD 1 TO THE ELEMENT OF A KEY
   end repeat
   answer tWordCount["aa"]
end mouseup
This makes a bunch of random words, and counts how many times "aa" appears.

Practice, practice...

Craig Newman

antrax13
Posts: 39
Joined: Mon Jun 01, 2015 11:38 am

Re: Creating/Populating Associative Array

Post by antrax13 » Wed Aug 26, 2015 2:15 pm

OK got what I need here is the code that was needed :)

Code: Select all

   put 1 into counter
   repeat for each element e in AvailableArray
      put e["id"] into testArray[counter]["id"]
      put e["description"] into testArray[counter]["description"]
      add 1 to counter
   end repeat
Thanks to myself :D

Post Reply