Re: Need help to write routine - Please

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

Tony wrote:

> Hello Juergen,
> I might have been a little premeture because you code actually fails.
> I need to change code slightly but when I do it falls over. Below is
> what I used. You will notice I build all the newValues & this seems to
> cause the problem

'positionsToChange' must contain _all_ positions, that are subject to
change, i.e. it must always have the same length as 'newValues'
(see below).

> Regards
> Tony
>
> sequence mainSequence, positionsToChange, newValues
> integer Level
>
> procedure change ()
>    for posn = 1 to length(newValues[Level]) do
>       mainSequence[positionsToChange[Level]] = newValues[Level][posn]
>       if Level < length(positionsToChange) then
>          Level += 1
>          change()
>          Level -= 1
>       else
>          ? mainSequence
>       end if
>    end for
> end procedure
>
>
> mainSequence = {0,5,3,4,1,1}
> positionsToChange = {2,4}

When you change this to:
   positionsToChange = {1,2,3,4,5,6}
then it works.

> newValues = {
> 	       {2,4,6,8},
>              {1,3,7,9},
>              {1,5,7,9},
>              {0,2,6,8},
>              {3,5,7,9},
>              {3,5,7,9}
>             }
>
> Level = 1
> change()

<snipped old text>

Here is the code with a built-in security check for this problem
(and with a minor tweak, that makes it look a little more elegant,
and makes it even 4 lines shorter. smile

----------=-------------------------------------------------=----------
sequence mainSequence, positionsToChange, newValues

procedure change (integer Level)
   for posn = 1 to length(newValues[Level]) do
      mainSequence[positionsToChange[Level]] = newValues[Level][posn]
      if Level < length(positionsToChange) then
         change(Level+1)
      else
         ? mainSequence
      end if
   end for
end procedure


mainSequence = {0,5,3,4,1,1}
positionsToChange = {1,2,3,4,5,6}
newValues = {
             {2,4,6,8},
             {1,3,7,9},
             {1,5,7,9},
             {0,2,6,8},
             {3,5,7,9},
             {3,5,7,9}
            }

if length(positionsToChange) = length(newValues) then
   change(1)
else
   puts(1, "Error: 'positionsToChange' and 'newValues' must always have"
         & " the same length.")
end if
----------=-------------------------------------------------=----------

Enjoy!

Regards,
   Juergen

-- 
 /"\  ASCII ribbon campain  |    |\      _,,,---,,_
 \ /  against HTML in       |    /,`.-'`'    -.  ;-;;,_
  X   e-mail and news,      |   |,4-  ) )-,_..;\ (  `'-'
 / \  and unneeded MIME     |  '---''(_/--'  `-'\_)

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

Search



Quick Links

User menu

Not signed in.

Misc Menu