Re: limitations of sequences, revisited

new topic     » goto parent     » topic index » view thread      » older message » newer message

George Henry wrote:

<snip>
>
> Mike Nelson answered my queries by offering a couple of tools:
>
> >where z is root[RIGHTCHILD][LEFTCHILD][LEFTCHILD][RIGHTCHILD][VALUE]
> >... is there any way of modifying z without hardcoding that
> >particular expression? There does not appear to be a way of "aliasing"
> >that nasty expression, or saving some sort of bookmark to z that I can >
> >use later to access it.
>
> <begin Mike's code>
> sequence bookmark
> bookmark={RIGHTCHILD,LEFTCHILD,LEFTCHILD,RIGHTCHILD,VALUE}
>
> -- then to retrieve:
>
> function subscript(sequence Param,sequence subsc)
>    object returnValue
>    returnValue=Param
>    for i=1 to length(subsc) do
>       returnValue=returnValue[subsc[i]]
>    end for
>    return returnValue
> end function
>
> object val
> val=subscript(root,bookmark)
> <end Mike's code>
>
> Ah, so then can I write:
>
> subscript(root, bookmark) = 'x'
>
> No! And my "complaint" was that I cannot use such techniques to MODIFY a
> value deeply buried within the structure. I could, for example. try this:
>
> sequence bookmark
> bookmark={RIGHTCHILD,LEFTCHILD,LEFTCHILD,RIGHTCHILD}
>

Here's a solution in the style of subscript():

function modify_subscript(sequence seq,sequence sub,object val)
   integer len
   len=length(sub)
   if len>1 then
      seq[sub[1]]=modify_subscript(seq[sub[1]],sub[2..len],val)
   elsif len=1 then
      seq[sub[1]]=val
   end if
   return seq
end function

then we can write:

root=modify_subscript(root,bookmark,newvalue)

The use of recursion handles the modifcations without needing advance
knowledge of the sequence depth.  BTW, recursion is fairly cheap in
Euphoria.

Perhaps the functions would be better named get_subscript and set_subscript.

-- Mike Nelson

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu