Library for using associative arrays as indexed arrays

Collaborate on tools, libraries, and applications beyond the IDE

Moderators: Klaus, FourthWorld, heatherlaine, kevinmiller, robinmiller

Post Reply
Lagi Pittas
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 248
Joined: Mon Jun 10, 2013 1:32 pm

Library for using associative arrays as indexed arrays

Post by Lagi Pittas » Mon Feb 13, 2017 4:33 pm

Hi

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?)

Regards Lagi

Code: Select all

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

Code: Select all

on TestSquish
   local lcA, lcNewA
   
   put "ZZZ" into lcA[26]
   put "AAA" into lcA[1]
   put "GGG" into lcA[8]
   put "BBB" into lca[2]
   put "EEE" into lcA[5]
   
   put SquishArray(lcA) into lcNewA
   
   put PrintA(lcNewA,true)
end TestSquish

function  PrintA pA, pWithKeys
   local lcArray, lcKeys
   
   put the keys of pA into lcKeys
   put pA into lcArray
   combine lcArray using  return and ":"
   return  lcArray
end PrintA

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 5379
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: Better way to "Squish" array elements to be contiguous?

Post by FourthWorld » Mon Feb 13, 2017 6:03 pm

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 Liaison

LiveCode development, training, and consulting services: Fourth World Systems: http://FourthWorld.com
LiveCode User Group on Facebook : http://FaceBook.com/groups/LiveCodeUsers/

Lagi Pittas
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 248
Joined: Mon Jun 10, 2013 1:32 pm

Re: Better way to "Squish" array elements to be contiguous?

Post by Lagi Pittas » Mon Feb 13, 2017 6:25 pm

Hi Richard

Should be interesting especially if Thierry comes in with a 10 character Regex or HH with a recursive routine that also spits out the lottery numbers as a side-effect :wink:

Go ahead please, it will I hope bring some nice ideas from the more experienced crew

Regards Lagi

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 5379
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: Library for using associative arrays as indexed arrays

Post by FourthWorld » Mon Feb 13, 2017 7:59 pm

Done, Lagi. I also took the libery of changing the thread subject line to better reflect the scope of this project.
Richard Gaskin
Community volunteer LiveCode Community Liaison

LiveCode development, training, and consulting services: Fourth World Systems: http://FourthWorld.com
LiveCode User Group on Facebook : http://FaceBook.com/groups/LiveCodeUsers/

AxWald
Posts: 287
Joined: Thu Mar 06, 2014 2:57 pm

Re: Library for using associative arrays as indexed arrays

Post by AxWald » Sun Feb 19, 2017 2:23 pm

Hi,

If you don't need sorting & just want an index w/o holes, use this:

Code: Select all

function reindexArr theArr
   put 1 into myNum
   repeat for each line myKey in the keys of theArr
      put theArr[myKey] into newArr[myNum]
      add 1 to myNum
   end repeat
   return newArr
end reindexArr
On my machine the times for 10.000 iterations are (6.7.10, your example):
< 170 ms unsorted; < 228 ms sorted (your squish)

Rarely used by me; found this function recently & am still wondering what for I wrote it ;-)

Have fun!
Livecode programming until the cat hits the fan ...

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 1538
Joined: Thu Feb 28, 2013 11:52 pm
Location: Göttingen, DE

Re: Library for using associative arrays as indexed arrays

Post by [-hh] » Mon Feb 20, 2017 3:07 pm

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

(Script of a demo push button, you need also fields 1 and 2).

Code: Select all

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
shiftLock happens

capellan
Posts: 379
Joined: Wed Aug 15, 2007 11:09 pm
Contact:

Re: Library for using associative arrays as indexed arrays

Post by capellan » Mon Feb 20, 2017 8:06 pm

Hi All,

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.

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 1538
Joined: Thu Feb 28, 2013 11:52 pm
Location: Göttingen, DE

Re: Library for using associative arrays as indexed arrays

Post by [-hh] » Mon Feb 20, 2017 9:55 pm

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:

Lagi, think about the indexing method for your array ...
shiftLock happens

capellan
Posts: 379
Joined: Wed Aug 15, 2007 11:09 pm
Contact:

Re: Library for using associative arrays as indexed arrays

Post by capellan » Tue Feb 21, 2017 12:34 am

Does anyone remembers Rob Cozens library? :shock:

slylabs13
Posts: 10
Joined: Thu Jul 26, 2007 7:23 pm
Contact:

Re: Library for using associative arrays as indexed arrays

Post by slylabs13 » Tue Feb 21, 2017 4:37 pm

It occurs to me that the Datagrid library must already do this. If you delete a line, don't the following lines renumber themselves?

Post Reply

Return to “Community Projects”