Split command key order

Mark Waddingham 36degrees at runrev.com
Thu Mar 3 07:24:43 EST 2005


> I know that the "combine" command will yield a list that is not in
> any particular order. However, I always thought the "split" command
> would split out the keys in the order of the original list.

The thing to remember here is that in order to get the keys of an array
you need to use the keys() function - and the engine does not make any
guarantee of the ordering that this function will return (the arrays
formed by split are not special in any way).

The reason for this, as Jacque has mentioned, is due to arrays being
implemented internally as a hash-table, and as such the ordering depends
on the hash function used.

If you have dense purely numeric keys, you can find out the minimum and
maximum values present by using the 'extents' function and then a
'repeat with i = ...' type construct to iterate through in order:

  local tMin, tMax, tIndex
  put item 1 of line 1 of the extents of tArray into tMin
  put item 2 of line 1 of the extents of tArray into tMax
  repeat with tIndex = tMin to tMax
    put "[" & tIndex & "] = " & tArray[tIndex] & return after message
  end repeat

If you have sparse purely numeric keys, then you are better off sorting
the keys:

  local tKeys, tIndex
  put the keys of tArray into tKeys
  sort lines of tKeys ascending numeric
  repeat for each line tIndex in tKeys
    put "[" & tIndex & "] = " & tArray[tIndex] & return after message
  end repeat

Otherwise, you must have alphanumeric keys, in which case just sort in
the normal way:

  local tKeys, tKey
  put the keys of tArray into tKeys
  sort lines of tKeys ascending
  repeat for each line tKey in tKeys
    put "[" & tIndex & "] = " & tArray[tKey] & return after message
  end repeat

Of course, now I've just written all of that I'm thinking - "Y'know, for
Transcript, that all seems awfully verbose... I wonder if we can do any
better in the future...".

Warmest regards,

Mark.

------------------------------------------------------------------
 Mark Waddingham ~ 36degrees at runrev.com ~ http://www.runrev.com
       Runtime Revolution ~ User-Centric Development Tools



More information about the use-livecode mailing list