Put "I just don't get it" into Array["Failure

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
el_stupido
Posts: 24
Joined: Tue Jun 23, 2009 9:38 am

Put "I just don't get it" into Array["Failure

Post by el_stupido » Mon Oct 19, 2009 3:07 pm

I get the concept of arrays and I could actually do the following just using variables and some cheeky tricks changing the delimiter, although I read in the manual that arrays are faster, so I thought I'd try a simple experiment... ANY advice would be so appreciated. Cause so far and with limited documentation and basically I'm stuck...

Here's the test data

Code: Select all

1045#0000#TRUE,1045#0000#TRUE,1045#0000#TRUE
1045#0000#TRUE,1045#0000#FALSE,1045#0000#TRUE
1045#0000#TRUE,1045#0000#TRUE,1045#0000#TRUE
Let's say that data starts off initially in a field

Basically I want to create a 3 by 3 array, lets call it 'map' and at each point in that array be able to access the 3 properties stored at that x, y co-ord let's call that 'map_metadata'

I've used the comma & return to separate the x, y points and the # to delimit the metadata.

how do I get this to go into the array and sub array properly?

once it's in there how can i look at for instance: co-ord 2,2 & see that item 3 returns FALSE?

how do I make arrays and their sub elements global?

any help would be very much appreciated

thankyou

El Stupido

Philhold
Posts: 64
Joined: Thu Mar 05, 2009 3:39 pm

Post by Philhold » Mon Oct 19, 2009 3:43 pm

I'm a complete novice at this but here goes:

Make a new stack with a button and a field. Attach this script to the button.

Code: Select all

on mouseUp pMouseBtnNo
put "1045" into myArray[1][1]
put "0000" into myArray[1][2]
put "TRUE" into myArray[1][3]
put myArray[1][3] into field 1
end mouseUp
Puts TRUE into the field.

I think that should get you started.

To put data into an array you simply tell RunRev where to put it. ARRAYNAME[key][key]...

As far as I know you can call the array anything and it doesn't have to include the word "array" and your keys can be words or numbers.

There's a very useful article on this subject by Trevor DeVore here:
http://runrev.com/newsletter/july/issue ... etter1.php

And by Tom Healy here
http://www.runrev.com/newsletter/septem ... etter1.php

Cheers

Phil

SparkOut
Posts: 2943
Joined: Sun Sep 23, 2007 4:58 pm

Post by SparkOut » Mon Oct 19, 2009 4:53 pm

I prepared this, then got sidetracked, but broadly, since I'd done it and it also mentions the value of the RevUp article by Tom Healy, I'll post it anyway. It also shows that there is a possible rethink about the metadata elements needed, as Phil uses 1045 and 0000 like row and column values (which may be more appropriate, depending on what you need).

You can think of the x,y coordinates as a row number and a column index in this case, each being one element of the 3 level array. Each level of the array can have its own set of values for each key. (Not making myself very clear, I know - skip to the RevUp article for much better understanding!)

So you could

Code: Select all

put 1045 into mapA[1][1]["metadata1"]
put "0000" into mapA[1][1]["metadata2"]
put TRUE into mapA[1][1]["metadata3"]
put 1045 into mapA[1][2]["metadata1"]
put "0000" into mapA[1][2]["metadata2"]
put TRUE into mapA[1][2]["metadata3"]
put 1045 into mapA[1][3]["metadata1"]
put "0000" into mapA[1][3]["metadata2"]
put TRUE into mapA[1][3]["metadata3"]
put 1045 into mapA[2][1]["metadata1"]
put "0000" into mapA[2][1]["metadata2"]
put TRUE into mapA[2][1]["metadata3"]
put 1045 into mapA[2][2]["metadata1"]
put "0000" into mapA[2][2]["metadata2"]
put TRUE into mapA[2][2]["metadata3"]
put 1045 into mapA[2][3]["metadata1"]
put "0000" into mapA[2][3]["metadata2"]
put TRUE into mapA[2][3]["metadata3"]
put 1045 into mapA[3][1]["metadata1"]
put "0000" into mapA[3][1]["metadata2"]
put TRUE into mapA[3][1]["metadata3"]
put 1045 into mapA[3][2]["metadata1"]
put "0000" into mapA[3][2]["metadata2"]
put TRUE into mapA[3][2]["metadata3"]
put 1045 into mapA[3][3]["metadata1"]
put "0000" into mapA[3][3]["metadata2"]
put TRUE into mapA[3][3]["metadata3"]
You can of course make this more efficient in code lines by looping through to set the variable data.
Now say you wanted to get the value stored in the third metadata item of coordinate 2,1 (which should be "TRUE"). You can

Code: Select all

put mapA[2][1]["metadata3"]
You may find it much more easy to follow and more natural/powerful/flexible if you understand that in RunRev arrays are "hash tables" and not necessarily numerically indexed. So you could have

Code: Select all

put "bread,cheese" into recipeA["Cheese on Toast"]["ingredients"]
put "egg" into recipeA["Boiled Egg"]["ingredients"]
A much better example, with deeper levels is in this RevUp article by Tom Healy: http://runrev.com/newsletter/september/ ... etter1.php

HTH,
SparkOut

el_stupido
Posts: 24
Joined: Tue Jun 23, 2009 9:38 am

Thanks SparkOut

Post by el_stupido » Tue Oct 20, 2009 12:08 am

the coin dropped in what I was doing wrong.

I have it all humming very nicely now

thanks to all.

Stupes

el_stupido
Posts: 24
Joined: Tue Jun 23, 2009 9:38 am

Update: Thanks and code for all

Post by el_stupido » Mon Oct 26, 2009 7:49 am

thanks for everyone who offered up help. I totally got arrays in the end. Wow they are fast. Basically I have to set up an array of 12 x 12 points, each of those points has about 8 pieces of meta data and the 12 x 12 points are then grouped in x by y, say like 15 x 15 sets of 12 x 12 grids, with 8 bits of meta-data per point.

My first attempt wrote this data to a variable and that too so long to run thru. The version included in this populated almost straight away.

fantastic result.


Code: Select all

-- data init (in real prog user defined)
put 15 into y_screens
put 15 into x_screens

-- determine the number of addresses for map based on each screen being 12 x 12 

put y_screens into y_max
put x_screens into x_max
multiply y_max by 12
multiply x_max by 12 

-- populate the array

repeat with y = 1 to y_max -- y dimension of array
    repeat with x = 1 to x_max -- x dimensions of array
            repeat with meta = 1 to 8 -- z dimensions of array/meta data 
                put "map data here" into map[y][x][meta]
            end repeat
    end repeat
end repeat

Anyway thanks heaps to all, hope this code helps

El Stupido

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10043
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: Update: Thanks and code for all

Post by FourthWorld » Mon Oct 26, 2009 2:10 pm

el_stupido wrote:My first attempt wrote this data to a variable and that too so long to run thru.
To better understand the differences, would you consider posting the older, slower code?
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

el_stupido
Posts: 24
Joined: Tue Jun 23, 2009 9:38 am

Previous code

Post by el_stupido » Tue Oct 27, 2009 8:30 am

Sure... here's the earlier code. This code used variable instead of array

therefore to achieve the 3 dimensional array I made line y, item x & then changed the delimiter so that the data stored in each item was separated by a #

for example here is 3 lines with 3 items and 3 bits of metadata

Code: Select all


01#meta2#meta3, 02#meta2#meta3, 03#meta2#meta3
04#meta2#meta3, 05#meta2#meta3, 06#meta2#meta3
07#meta2#meta3, 08#meta2#meta3, 09#meta2#meta3

and the code to read that would be

Code: Select all

   repeat with y = 1 to 3
      repeat with x = 1 to 3
         put item x of line y of var_simplemap into tile_current 
         set the itemdelimiter to numtochar(35) -- sets delimiter to # char
         repeat with meta_z = 1 to 3
            answer item meta_z of tile_current
         end repeat
         set the itemdelimiter to numtochar(44) -- sets back to comma
      end repeat
   end repeat
[/code]

el_stupido
Posts: 24
Joined: Tue Jun 23, 2009 9:38 am

sorry

Post by el_stupido » Tue Oct 27, 2009 8:36 am

sorry I realise that code isn't quite the same...

Code: Select all

-- data init (in real prog user defined) 
put 15 into y_screens 
put 15 into x_screens 

-- determine the number of addresses for map based on each screen being 12 x 12 

put y_screens into y_max 
put x_screens into x_max 
multiply y_max by 12 
multiply x_max by 12 

--- var version

repeat with y = 1 to y_max -- y dimension of array 
    repeat with x = 1 to x_max -- x dimensions of array 
            set the itemdelimiter to numtochar(35) -- sets delimiter to # char 
            repeat with meta = 1 to 8 -- z dimensions of array/meta data 
                put "map data here" into item meta of mappoint_meta 
            end repeat 
            set the itemdelimiter to numtochar(44) -- sets back to comma 
            put mappoint_meta into item x of line y of map 
    end repeat 
end repeat 


Post Reply