Arrays: new and old keys, i

Ken Ray kray at sonsothunder.com
Fri Sep 12 13:24:08 EDT 2008


> Can anybody explain what the new array format provides that the old
> did not?  All these bizarre examples seem not so much as exemplifying
> the ``new'' features as to leave me baffled as to what added value
> they provide.  In NONE of the supposed or alleged examples have I seen
> anything I could not do with old arrays.  So, and I mean in really
> simple language (not OOP-speak), wtf is up with these new arrays?  I
> see nothing but extra brackets.

Well it's not so much what you can do that you couldn't do before, but
primarily the *ease* at which you can do things now.

Here's what I mean... suppose that you were keeping track of parts and
components for those parts that were being ordered, along with the style of
the component and the quantity ordered. Assume also there are thousands of
parts and thousands of components for those parts that could have been
ordered. 

In the old format, I might set up an array with this format using
underscores as a separator (I use the "A" suffix on the variable to indicate
it's an array):

  myOrderA[<part>_<component>_<style>]  => value of element is quantity

So I'd do this:

  put "2" into myOrderA["widget_mountingscrew_standard"]
  put "1" into myOrderA["widget_mountingscrew_flattop"]
  put "2" into myOrderA["widget_bracket_small"]
  put "3" into myOrderA["thingimajig_bracket_large"]

If I wanted to get a simple unique list of what components were ordered for
the "widget" part from this array, I'd have to loop through all the keys in
the array:

local myOrderA
answer GetComponents("widget")

function GetComponents pPart
  put "" into tList
  set the itemDel to "_"
  repeat for each line tKey in (the keys of myOrderA)
    if item 1 of tKey = pPart then
      if item 2 of tKey is not among the lines of tList then
        put item 2 of tKey & cr after tList
      end if
    end if
  end repeat
  delete char -1 of tList  -- remove trailing CR
  return tList
end GetComponents

With the new array structure, the data would be entered like this:

 put "2" into myOrderA["widget"]["mountingscrew"]["standard"]
  put "1" into myOrderA["widget"]["mountingscrew"]["flattop"]
  put "2" into myOrderA["widget"]["bracket"]["small"]
  put "3" into myOrderA["thingimajig"]["bracket"]["large"]

and you'd get the list of components for the "widgets" this way:

function GetComponents pPart
  return the keys of myOrderA[pPart]
end GetComponents

That's just one example...

Ken Ray
Sons of Thunder Software, Inc.
Email: kray at sonsothunder.com
Web Site: http://www.sonsothunder.com/





More information about the use-livecode mailing list