I have been loosely following this thread and thought I would share my first use of C_OBJECT. Many years ago I inherited a 4D database that David Adams, yes you David, had done some work on. Specifically he had crated a web interface for it and used much of what he talked about in one of his early books on the subject of 4D and the web.
David used a set of routines to parse both incoming URL parameters name value pairs, as well as form field name value pairs into parallel arrays. I just recently modified his code to instead parse them into C_Objects. I did this as I was about to implement a very large complex query to respond to requests for records from a table of 50,000 records with up to 30 possible filters or search parameters involving multiple related tables. I must say that using C_Objects for the query made building the query much easier and with fewer lines of code. In the web app the filters are loaded into Dictionaries, which I combine into a single dictionary which is sent as a form in the request to 4D. Parsed into a C_Object… QUERY([Project];[Project]ID>0;*) If (OB Get(oFormFieldPrameters;"pfTitle")#"") QUERY([Project]; & ;[Project]Project_Title=OB Get(oFormFieldPrameters;"pfTitle";*)) End if If (OB Get(oFormFieldPrameters;"pfConsultants")#"") QUERY([Project]; & ;[Project]Consultants=OB Get(oFormFieldPrameters;"pfConsultants";*)) End if //..... etc. 30 times QUERY([Project]) Could of done it with the parallel arrays, but this is far more elegant and perhaps even more efficient. John > On Jul 19, 2017, at 11:39 PM, JPR via 4D_Tech <4d_tech@lists.4d.com> wrote: > > > > [JPR] > > Hi Guys, > > The exact thing that I've explained was how to use objects to get Associative > Arrays in 4D. Associative Arrays are widely used in other languages like PHP > or JavaScript. > > In computer science, an Associative Array is an abstract data type composed > of a collection of (key, value) pairs, such that each possible key appears > just once in the collection. In 4D, the JSON-type Objects are perfect for > Associative Arrays. > > In many case, you will have 2 parallel arrays, let's say one for the product > code ($arCodes), and one for the product name ($arNames). You want to find > the name for a specific code (classic 4D way) > > - You create the array with a loop, adding elements pairs $myCode and $myName: > > $k:=Find in array($arCodes;$myCode) > If ($k>0) > $arNames{$k}:=$myName > Else > APPEND TO ARRAY($arCodes;$myCode) > APPEND TO ARRAY($arNames;$myName) > End if > > - Then you can find a name from a code: > > $k:=Find in array($arCodes;$myCode) > If ($k>0) > $myName:=$arNames{$k} > Else > $myName:="" > End if > > Now if you use an Object: > > C_OBJECT($myArray) > > You create the array with a loop: > > OB SET($myArray;$myCode;$myName) > > -and you find with: > > $myName:=OB Get($myArray;$myCode} > > ...much simpler, and much faster! Why is it faster? With a classic array, the > Find in array command has to parse the entire array, element per element, > until the correct element is found (Except in case of a Sorted array, but > sometimes you can't sort the array because the index of an element can be > meaningful for your method) > > In case of an object, the properties are 'indexed' by using an internal Hash > table, so the access to one particular Property doesn't need a sequential > parsing of the list of values, but an almost direct access. I confirm what > Justin says, that is to say that the bigger will be the array, the more > efficient will be associative arrays compared with classic parsing of arrays. > > My very best, > > JPR > > > >> Message: 7 >> Date: Mon, 17 Jul 2017 11:43:12 -0700 >> From: Justin Leavens <jus...@jitbusiness.com> >> To: 4D iNug Technical <4d_tech@lists.4d.com> >> Subject: Re: Arrays vs Object for Key/Value pair lookups >> Message-ID: >> <CABwcA3un5GoYVWrFeRXzpm8bNuOtSdaymO9=byk-d_c1vp5...@mail.gmail.com> >> Content-Type: text/plain; charset="UTF-8" >> >> I did a 2014 Summit presentation (5 JSON Tips) which should be available >> for download that demonstrated the benefits of using objects for key/value >> pair cache lookups, but in the end it’s pretty easy to demonstrate. The >> benefits start to show up with a few hundred keys, but at 100,000 it’s >> easily 20x faster looking up object keys as opposed to find in array in >> interpreted. And when you compile, it’s literally hundred of times faster >> (400-500x) at 100k keys - and the benefits just get bigger and bigger with >> more keys. That’s both for filling the cache and for retrieving values >> (objects save you from having to check if a key is already in the array >> before adding it). >> >> -- >> Justin Leavens >> jus...@jitbusiness.com (818) 986-7298 x <//(818) 986-7298 x301>701 >> Just In Time Consulting, Inc. >> Custom software for unique businesses >> http://www.linkedin.com/in/justinleavens >> >> On July 17, 2017 at 3:46:26 AM, Peter Jakobsson via 4D_Tech ( >> 4d_tech@lists.4d.com) wrote: >> >> Hi >> >> I remember at last year’s summit, JPR was emphasising how objects were far >> more optimised than arrays for doing lookups over large numbers of key >> value pairs. >> >> e.g. we usually do this: >> >> $x:=find in array(myKEYS;”product_code_x”) >> >> if($x>0) >> $0:=myPRICES{$x} >> end if >> >> How do people prefer to do this with objects ? Enumerate the keys in some >> systematic way and then populate the object like this > >> >> For($i;1;$SIZE) >> >> $key:=string($i) >> $value:=myarrayVAL{$i} >> OB SET($object;$key;$value) >> >> End For >> >> Then for retreiving: >> >> $key:=string($1) >> >> $0:=OB Get($object;$key) >> >> …or was JPR suggesting we use object arrays and do some kind of “find” over >> the object arrays ? >> >> Best Regards >> >> Peter >> > > ********************************************************************** > 4D Internet Users Group (4D iNUG) > FAQ: http://lists.4d.com/faqnug.html > Archive: http://lists.4d.com/archives.html > Options: http://lists.4d.com/mailman/options/4d_tech > Unsub: mailto:4d_tech-unsubscr...@lists.4d.com > ********************************************************************** ********************************************************************** 4D Internet Users Group (4D iNUG) FAQ: http://lists.4d.com/faqnug.html Archive: http://lists.4d.com/archives.html Options: http://lists.4d.com/mailman/options/4d_tech Unsub: mailto:4d_tech-unsubscr...@lists.4d.com **********************************************************************