1. Constructing an array value from a sequen - Reply

doug edmunds wrote:

>I am looking for suggestions how to read and write
>a value into an object containing sequences
>at a particular array location given a sequence of integers.

>For example:
>   I have a sequence called  'my_data' which containing sequences
>   within sequences.  The procedures  will
>  read the value of a selected part of 'my_data' and
>  write a value to that location.
>
>Header of procedures:
>  procedure Read_Data (sequence my_data, sequence data_location)
>  procedure Write_Data (sequence my_data, sequence data_location)
>
>Required criteria of 'my_data'
>         The data object will not have a fixed record length, but rather
>will contain
>         variable size lists (within variable size lists):
>
>For example if my_data equals:
> {{"dog",{"beagle","spaniel", {"shepherd",{"large","small"}}},
>{"colors",{"black","white"}}}
>then my_data[1][2][3] = {"shepherd",{"large","small"}}
>and has a sequence representation of  {1,2,3}
>
>my_data[1][2][3][1] is equal to  "large" (has the sequence value of
>{1,2,3,1})


Doug, believe me, it helps when questions are more carefully formulated: there
are unmatched braces in your example, and my_data[1][2][3][1] appears to be
"shepherd", not "large" as you stated. Also, since Euphoria does not allow
passing of parameters by reference, your routines will have to be probably
functions, rather than procedures.

If I have not completely misunderstood you, a rather clumsy solution is attached
below. But I am sure more efficient and elegant (recursive?) alternatives are on
the way...

Btw, I have a feeling, the same, or very similar subject was discussed on this
list several times already. Someone, perhaps, should volunteer to start a FAQ
file. Once it is established, it could become a collective effort. Jiri

-- snip ------------------------------------------------------------------------

function read_data(object data, sequence loc)
   for i=1 to length(loc) do
      data=data[loc[i]]
   end for
   return data
end function

function write_data(object data, sequence loc, object item)
   object s
   integer len
   len=length(loc)
   s={data}
   if len>1 then
      for i=1 to len-1 do
         s=append(s,s[i][loc[i]])
      end for
   end if
   s=append(s,item)
   for i=len to 1 by -1 do
      s[i][loc[i]]=s[i+1]
   end for
   return s[1]
end function

new topic     » topic index » view message » categorize

2. Re: Constructing an array value from a sequen - Reply

> >For example:
> >   I have a sequence called  'my_data' which containing sequences
> >   within sequences.  The procedures  will
> >  read the value of a selected part of 'my_data' and
> >  write a value to that location.
> >
> If I have not completely misunderstood you, a rather clumsy solution is
> attached below. But I am sure more efficient and elegant (recursive?)
> alternatives are on the way...
>
> Btw, I have a feeling, the same, or very similar subject was discussed on this
> list several times already. Someone, perhaps, should volunteer to start a FAQ
> file. Once it is established, it could become a collective effort.
>

I think I solved it recursively in the newer slice.e (it's there because I
needed it in tree.e) Recursion solves some problems, but avoid it if you can,
because it eats stack for breakfast! Your version for getting stuff out of a
seq is better than mine. (I think, it's been a while, an I skimmed yr code)

I think maybe I should overhaul slice.e... (tree.e works but is crummy, and
since noone seems to use it...)

>> doug edmunds
>Jiri
Anders

-------------------------------------------------------------------
Anders Eurenius <c96aes at cs.umu.se> ICQ UIN:1453793
Computer Science/Engineering student at the university of Umeaa

In a world without fences, who needs Gates
-------------------------------------------------------------------

new topic     » goto parent     » topic index » view message » categorize

3. Re: Constructing an array value from a sequen - Reply

Jiri Babor writes:
> If I have not completely misunderstood you,
> a rather clumsy solution is attached below.
> But I am sure more efficient and elegant (recursive?)
> alternatives are on the way...

Here's a recursive version ...

function write_data(object data, sequence loc, object item)
    if length(loc) = 0 then
        return item
    else
        data[loc[1]] = write_data(data[loc[1]], loc[2..length(loc)], item)
        return data
    end if
end function

-- examples:
sequence s, t
s = {{{1,2,3}, {4,5,6}}, 7, 8}
t = {9}
? write_data(t, {1}, -9)       -- {-9}
? write_data(s, {1,2,3}, 999)  -- {{{1,2,3}, {4,5,999}}, 7, 8}
? write_data(888, {}, 999)     -- returns 999

In the last case Jiri's write_data() returns 888.
I don't know which is better.

As with most recursive solutions, you have to scratch your head
for a minute to figure out how it works.

Regards,
  Rob Craig
  Rapid Deployment Software

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu