I have an array of line items where the user can delete line at a time and I have a parallel Array/customproperty that hold other information. The routines that I have, before adding this request assumed the lines were contiguous.

So searched in the dictionary to see if tere was a way of Squishing my array after the element was removed but I came up with nothing that I can see. So I quickly whipped up something that works perfectly for my situation but it doesn't seem elegant or in anyway "right" when we have associative arrays and nothing built in to "cleanup" when we delete elements.

Has anybody got a better/simpler way - there have to be at least 5 methods (it's Livecode isn't it?)

function SquishArray pArray
local lcNewA, lnMaxKeys, lnX, lnNewX, lcKeys, lnOldIndex
get the keys of pArray
put the number of lines in it into lnMaxKeys
put it into lcKeys
sort lines of lcKeys Ascending Numeric
repeat with lnX = 1 to lnMaxKeys
add 1 to lnNewX
put line lnX of lcKeys into lnOldIndex
put pArray[lnOldIndex] into lcNewA[lnNewX]
end repeat
return lcNewA
end SquishArray

Good work, Lagi. Like most associative array implementations, LiveCode's arrays are an unordered collection of key-value pairs, so the automatic rekeying of serial integer keys we see with indexed arrays (often called "proper lists") are not reflective of how associative arrays manage their pointers, requiring us to write some code when we want that behavior.

I wonder if it may be a worthwhile community project to flesh this out into a complete set of CRUD operations for using LC's associative arrays like indexed arrays.

Your SquishArray seems a good start for handling deletions, so next would be handling the addition of new elements as well.

If you're up for stewarding such a project I'd be happy to move this thread into the Community Projects section of the forums.

Richard Gaskin
Community volunteer LiveCode Community LiaisonLiveCode development, training, and consulting services: Fourth World Systems:http://FourthWorld.comLiveCode User Group on Facebook :http://FaceBook.com/groups/LiveCodeUsers/

As you are asking for solutions that don't return lottery numbers, here is another one.
Just tell me if it is fast enough to add a random lottery drawing

The following button script gives a usage example of the three utility functions for
= building an indexedArray from a list of sorted elements
= rebuilding a contiguous index after deleting an element from the indexedArray
= rebuilding a contiguous index after inserting an element from the indexedArray

local indexedArray
-- insert pElement at indexedArray[pNum] and rebuilds contiguous index
-- assumes that pNum <= 1+the number of elements of indexedArray
on reindexAfterInsert pNum,pElement
local tA
put indexedArray into tA
repeat for each key k in indexedArray
if k < pNum then next repeat
put indexedArray[k] into tA[k+1]
end repeat
put pElement into tA[pNum]
put tA into indexedArray
end reindexAfterInsert
-- delete indexedArray[pNum] and rebuilds contiguous index
on reindexAfterDelete pNum
local tA,tN
put indexedArray into tA
repeat for each key k in indexedArray
if k <= pNum then next repeat
put indexedArray[k] into tA[k-1]
end repeat
put item 2 of line 1 of the extents of tA into tN
delete variable tA[tN]
put tA into indexedArray
end reindexAfterDelete
-- builds indexedArray from a list of sorted elements
on buildIndexedArray pSortedElements
local tA
put pSortedElements into tA
split tA by cr -- change cr to another delimiter if elements contain cr
put tA into indexedArray
end buildIndexedArray
on mouseDown b
local tA,tAKeys,tSortedElements,tNum=3333
repeat with i=1 to 10000
put "_"& i &"_" into tA[i]
end repeat
put the millisecs into m1
##############################
if b = 3 then
## example 1: build indexedArray from sorted elements
put the keys of tA into tAKeys
sort tAkeys descending numeric -- an example sort
repeat for each line k in tAKeys
put cr & tA[k] after tSortedElements
end repeat
buildIndexedArray (char 2 to -1 of tSortedElements)
else
put tA into indexedArray
if the shiftkey is up then
## example 2: delete element index tNum of the indexedArray
reindexAfterDelete tNum
else
## example 3: insert an element at index tNum of the indexedArray
reindexAfterInsert tNum, "_0__________"
end if
end if
##############################
put the millisecs-m1 into fld 2
put indexedArray into tA
combine tA by cr and space
sort tA numeric by word 1 of each
put tA into fld 1
find tNum in fld 1
end mouseDown

Something tells me that I have seen similar handlers before. Could you check if similar handlers are already included in the stacks published by Rob Cozens for his Serendipity Library?

Read this announcement from 2004:

Rob Cozens wrote:

The Serendipity Library includes all the tools needed to create user-translatable applications in Runtime Revolution® and MetaCard®. It also introduces the world's first native X-Talk client/server database: Serendipity Database--Binary ("SDB").

The Library is a collection of Transcript commands for database operations, date manipulation, input editing, number formatting, and list & table manipulation. With the accompanying reference and associated files, it also serves as an exemplar of techniques for creating user-translatable applications and using SDB. It is available free of charge and can be distributed with any Runtime Revolution or MetaCard project royalty-free (see license terms for details).

The basic components of the Library are:

1. Serendipity_Library.rev, the library stack itself
2. Serendipity_Reference.rev, front end to the library documentation stack
3. SDB_Server.rev, a template to be used to create an SDB database server standalone for any platform
4. SDB_Tools.rev, a developer's plugIn stack supporting database & front end stack creation, data dictionary maintenance, and file utilities. See Serendipity Reference's section on SDB Tools' menus for details on each menu selection.
5. SDB_Front_End_Formats.rev, a stack of front end templates used by SDB Tools' New Front End Stack and New Front End Card menuItems
6. STAMPsdbClient.rev, a template to be used to create a test SDB database client standalone for any platform
7. SDB_Utilities.rev, a template to be used to create an SDB Utilities standalone for any platform
8. English_Reference_Text.sdb, an exemplar SDB database of library documentation accessed by Serendipity_Reference.rev
9. SDB_License_Terms.pdf, to be distributed with applications that use Serendipity_Library.rev but don't include
Serendipity_Reference.rev.
10. A folder, SDB_Message_Files, containing Library message translation files in Dutch, English, French, German, and Spanish.
11. ClickCalendar.rev, a stack containing a group of controls & handlers, plus scripting examples & documantation for capturing & displaying dates using an annual and/or single-month calendar. Month and day names are based upon the current system language of the computer ClickCalendar is running on; so translation is automatic except for some button toolTips. ClickCalendar uses handlers and images in Serendipity Library; thus any stack that includes the
ClickCalendar group must start using Serendipity_Library.rev before using the group
12. ClickClock.rev, a stack containing an animated gif and associated handlers, examples, and documentation for capturing & displaying time using an analog clock. ClickClock is self-contained, and thus does not need to have Serendipity_Library.rev in the stacksInUse to run.
13. libIPC.rev, the Revolution IPC Group's Transcript library of interprocess communications handlers.
14. SDB_Data.sdb, the default Client/Server test database.
15. Network_Setup.rev, a stack containing instructions for connecting two or more computers for IPC. The present version includes instructions for Apple computers only, and only for TCP/IP.
16. Builds, a folder containing Distribution Builder configuration stacks for SDB Utilities, SDB Server, and SDB Test Client.
17. HC_Address_Front_End.rev, a stack demonstrating SDB's HyperCard user interface
18. SDB_Address_Front_End.rev, a stack demonstrating SDB's SDB user interface
19. Sample_Report.rev, a report format stack used by Sample_Reporter.rev.
20. Sample_Reporter.rev, a stack demonstrating the use of Serendipity Library's printReportFromStack.
21. Array_Tester.rev, a stack demonstrating the use of Serendipity Library's getElement and putElement array handlers.

libSTAMP.rev, a modified prerelease version of the Revolution IPC Group's original library, is also distributed with this update. When the next version of libIPC is released, this file will no longer be necessary.

I am in the process of moving my Windows operations from SoftWindows on an iMac to a Windows XP Tablet PC, and until that process is completed the Windows self-expanding archive link is disabled. In the meantime, Windows developers can download and decompress the individual .sgz files by downloading & installing the SDB_Tools
plugIn at <http://wecode.org/serendipity/SDB_Tools.rev>...use the Decompress Files option of the File menu.

capellan wrote:Something tells me that I have seen similar handlers before.

Of course. These are simplest list utilities. The methods existed before the first computer was built.

For example, if you "delete" page 102 of a book and wish to renumber the pages you certainly won't touch the numbers of the pages pages 1 to 101...
And if you insert a page at index 102, you certainly won't touch the numbers of the pages 1 to 101. But you possibly start to think about "numbering" the inserted page with 101.1 (for example). Then you could leave the numbers of the pages 102 to -1 of the book also untouched: