Array question

Alex Tweedly alex at tweedly.net
Wed Jun 15 12:22:38 EDT 2005


CHARLES W SZASZ wrote:

>I am working on a stack that will have a mainstack and a substack for data. The mainstack will 
>will have two popup buttons, representing schools. I want to set it up so that the can get the 
>mileage going from one school (user selects a school that he is traveling from and selects 
>another school that he going to) and presses the Return key or presses the default button. How 
>do I code an array to give the mileage between two schools? There are about 80 schools.
>  
>
Not sure if I'm answering precisely the right question here or not - but 
here's a couple of answers.

I'm assuming that you will build the data from somewhere external (i.e. 
you aren't trying to calculate it based on ZIP code or from an address 
database). If you start with the distance info in a simple list, as in

Stanford, Harvard, 5000
Stanford, Berkeley, 50
Berkeley, Harvard, 4960
etc.


you could build an array, as in

-- for ease of use make the array twice as big as it needs to be, by 
storing both Stanford,Harvard  and also  Harvard,Stanford

  -- assuming all distances are same in both directions
   repeat for each line L in distanceList
      put item 3 of L into distArray[item 1 of L, item 2 of L] 
      put item 3 of L into distArray[item 2 of L, item 1 of L]
   end repeat

and the you could look up distances simply as, e.g.
    put distArray[choiceA, choiceB] into myVar

For only 6400 entries that would be fine - if you grew to storing large 
number of schools, you might want to encode the names (to save space), 
and you might want to store only half the array (to save space and 
marginally save look-up time)
 
  -- number all the schools, and store that number in array 
schoolNumber  (assumed to be done already)

  repeat for each line L in distanceList
    put schoolNumber[item 1 of L], schoolNumber[item 2 of L] into temp
    put item 3 of L into tDistance
    sort items of temp numeric
    put tDistance into distArray[temp]
 end repeat

and then look up by
  put choiceA, choiceB into temp   -- or put schoolNumber[choiceA], 
schoolNumber[choiceB] into temp
  sort items of temp
  put distArray[temp] into myVar

-- 
Alex Tweedly       http://www.tweedly.net



-- 
No virus found in this outgoing message.
Checked by AVG Anti-Virus.
Version: 7.0.323 / Virus Database: 267.7.3/15 - Release Date: 14/06/2005



More information about the use-livecode mailing list