Kirk,
go pull a copy of my component Txtutl
there are a methods there which can parse any text.
They include the term "parse"
https://www.dropbox.com/sh/ntmvl7k6mt7czv7/AAB2bl8vu6RqFdJHCqF_2VHma?dl=0
On Tue, 01 May 2018 20:31:26 +0000, Kirk Brooks via 4D_Tech wrote:
> Hi Julio,
> I was asking about how they are using Split string to parse a dot notation
> path into the value or containing object. Working this out was the real
> heavy lifting Canon did with his OBJ Module. It wasn't easy using the prior
> tool set.
>
> I played with it for a couple of hours and have come up with these two
> methods. I'm sure this can be improved on.
> First is this method to split a string into a collection of property
> elements. I also split the collection indices into numbers. BTW - this can
> also be used to make JSON Pointers.
>
> // Method: OBJ_parse_pathToCollection (text) => collection
> // $1 : dot notation path
> // $0 : collection of references
> C_TEXT($1;$path)
> C_COLLECTION($0;$myCol)
> C_LONGINT($i;$n)
>
> Case of
> : (Count parameters=0)
> : (Length($1)=0)
> Else
> $path:=Replace string(Replace string($1;"[";".");"]";"")
> $myCol:=Split string($path;".")
> For ($i;0;$myCol.length-1)
> If (Position($myCol[$i][[1]];"0123456789")>0) // starts with a number
> $myCol[$i]:=Num($myCol[$i])
> End if
> End for
> End case
> // --------------------------------------------------------
> $0:=$myCol
>
>
> Next is the method to extract the value or containing object of a path from
> an object or collection.
>
> // Method: OBJ_resolve_path (ptr; text) -> object
> // $1 : an object or collection
> // $2 : path to value, object or collection to return
>
> // $0 : {"success":(null, true or false), "value": (null, object,
> collection or value)}
>
>
> C_POINTER($1)
> C_TEXT($2;)
> C_OBJECT($resultObj;$0)
> C_LONGINT($i;$n)
> C_COLLECTION($dataCol;$pathCol)
>
> $pathCol:=OBJ_parse_pathToCollection ($2)
> $resultObj:=New object("success";Null;"value";Null)
>
> $dataCol:=New collection($1->)
> $i:=0
>
> Repeat
> Case of
> : (Value type($dataCol[0])=Is collection)
> // this must be a number
> Case of
> : (Value type($pathCol[$i])#Is real) // then this is not valid
> $resultObj.success:=False
> TRACE
> : ((($dataCol[0].length)-1)<$pathCol[$i]])
>
> $resultObj.success:=False
>
> : ($i=($pathCol.length-1)) // have reached the target
> $resultObj.success:=True
> $resultObj.value:=$dataCol[0][$pathCol[$i]]
> Else // go on to next property
> $dataCol.unshift($dataCol[0][$pathCol[$i]])
> End case
> : (Value type($dataCol[0])=Is object)
> Case of
> : (($dataCol[0][$pathCol[$i]])=Null)
> $resultObj.success:=False
> : ($i=($pathCol.length-1)) // have reached the target
> $resultObj.success:=True
> $resultObj.value:=$dataCol[0][$pathCol[$i]]
> Else // go on to next property
> $dataCol.unshift($dataCol[0][$pathCol[$i]])
> End case
> End case
> $i:=$i+1
> Until ($i>($pathCol.length-1)) | ($resultObj.success#Null)
>
> $0:=$resultObj
>
>
> $resultObj is an approach I'm starting to like a lot. It's the only way to
> return any kind of value, an object or a collection. The 'success' property
> is easy to test. This method currently returns 3 success values: true (the
> path evaluated), false (it doesn't), or null which means it's not valid
> either.
>
> I decided to use a pointer because I wanted to not have to determine the
> object type prior to calling the method - plus there would be two of them.
> Like I say, this can be improved.
>
> It is quite a lot less code than Canon's method required. Plus this deals
> with all values and objects or collections as the source.
>
> On Tue, May 1, 2018 at 7:39 AM Julio Carneiro via 4D_Tech <
> [email protected]> wrote:
>
>> Here is an example I’m using:
>>
>> C_COLLECTION($aOrderByCols)
>> $aOrderByCols:=Split string($orderBy;"%";sk ignore empty
>> strings+sk trim spaces)
>>
>> $orderBy is a text string with “%” separated terms, like “foo %bar%more
>> %%data”.
>>
>> As a result of the statement above, $aOrderByCols will become:
>> [“foo”,”bar”,”more”,”data”], as spaces and empty items are trimmed.
>>
>> That replaces a utility method í’ve had for decades.
>>
>> julio
>>
>>> On May 1, 2018, at 1:14 AM, Kirk Brooks via 4D_Tech <
>> [email protected]> wrote:
>>>
>>> Miyako,
>>> This occurred to me too. Sadly there's no example for that in the docs.
>>> Would you happen to have a bit of code showing how it's intended to be
>> used?
>>>
>>> On Mon, Apr 30, 2018 at 2:22 PM Keisuke Miyako via 4D_Tech <
>>> [email protected]> wrote:
>>>
>>>> the command "Split string", although generic,
>>>> was specifically created to help parse dot notation.
>>>>
>>>> http://doc.4d.com/4Dv16R6/4D/16-R6/Split-string.301-3644822.en.html
>>>>
>>>
>>> --
>>> Kirk Brooks
>>> San Francisco, CA
>>> =======================
>>>
>>> *We go vote - they go home*
>>> **********************************************************************
>>> 4D Internet Users Group (4D iNUG)
>>> FAQ: http://lists.4d.com/faqnug.html
>>> Archive: http://lists.4d.com/archives.html
>>> Options: https://lists.4d.com/mailman/options/4d_tech
>>> Unsub: mailto:[email protected]
>>> **********************************************************************
>>
>> --
>> Julio Carneiro
>>
>>
>>
>>
>>
>> --
>> Julio Carneiro
>> [email protected]
>>
>>
>>
>> **********************************************************************
>> 4D Internet Users Group (4D iNUG)
>> FAQ: http://lists.4d.com/faqnug.html
>> Archive: http://lists.4d.com/archives.html
>> Options: https://lists.4d.com/mailman/options/4d_tech
>> Unsub: mailto:[email protected]
>> **********************************************************************
>
>
>
> --
> Kirk Brooks
> San Francisco, CA
> =======================
>
> *We go vote - they go home*
> **********************************************************************
> 4D Internet Users Group (4D iNUG)
> FAQ: http://lists.4d.com/faqnug.html
> Archive: http://lists.4d.com/archives.html
> Options: https://lists.4d.com/mailman/options/4d_tech
> Unsub: mailto:[email protected]
> **********************************************************************
---------------
Gas is for washing parts
Alcohol is for drinkin'
Nitromethane is for racing
**********************************************************************
4D Internet Users Group (4D iNUG)
FAQ: http://lists.4d.com/faqnug.html
Archive: http://lists.4d.com/archives.html
Options: https://lists.4d.com/mailman/options/4d_tech
Unsub: mailto:[email protected]
**********************************************************************