Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #20415

Re: Looping holding old copy of array

Newsgroups comp.lang.javascript
Date 2013-06-24 03:38 -0700
References <6870a10e-ea81-40d5-91f8-ce425152e543@googlegroups.com> <xes4r9lujrjk.o8wrdbwo8ef9.dlg@40tude.net>
Message-ID <f33df78c-bb23-4cb7-b7eb-c7e3900cd12f@googlegroups.com> (permalink)
Subject Re: Looping holding old copy of array
From jonas.thornvall@gmail.com

Show all headers | View raw


Den måndagen den 24:e juni 2013 kl. 10:19:47 UTC+2 skrev JJ:
> On Sun, 23 Jun 2013 22:50:49 -0700 (PDT), jonas.thornvall@gmail.com wrote:
> 
> > I have a looping algorithm that must use least amount of work. 
> 
> > I am working on an array and that use a counter for loop behaviour (loop in a loop). 
> 
> > 
> 
> > However when loops finished, i need a copy of the array containing not the last item but the item before the last.  
> 
> > 
> 
> > Basicly i have two while loops
> 
> > while x>y
> 
> >     while  z>t
> 
> >     change Aarray
> 
> > 
> 
> > I want the content saved of the pre Aarray before loopend 
> 
> > 
> 
> > Here is my thought either i use       
> 
> > 
> 
> > while x>y
> 
> > newArray = Aarray.slice();
> 
> >     while  z>t
> 
> >     change Aarray
> 
> > 
> 
> > or i use
> 
> > 
> 
> > while x>y
> 
> > for(i=0;i<Aarray.length;i++){
> 
> >  newCopy[i] = Aarray[i];
> 
> >  }
> 
> >     while  z>t
> 
> >     change Aarray
> 
> > 
> 
> > I do not like to make a copy everytime, but is there away around it?
> 
> > Which of the two suggested methods is faster?
> 
> 
> 
> Save the array only when needed.
> 
> 
> 
> Assuming the original code is like this:
> 
> 
> 
> while (x > y) {
> 
>   newArray = Aarray.slice();
> 
>   while (z > t) {
> 
>     change(Aarray);
> 
>     z++;
> 
>   }
> 
>   x++;
> 
> }
> 
> 
> 
> Change it to:
> 
> 
> 
> newArray = Aarray.slice();
> 
> while (x > y) {
> 
>   if ((x+1) > y) {
> 
>     newArray = Aarray.slice();
> 
>   }
> 
>   while (z > t) {
> 
>     change(Aarray);
> 
>     z++;
> 
>   }
> 
>   x++;
> 
> }
> 
> 
> 
> The first array save will make sure that it'll be saved if the execution
> 
> never enters the "outer" loop.
> 
> 
> 
> The second array save will be executed only at the last "outer" loop
> 
> iteration.
> 
> 
> 
> If you use break anywhere in the "outer" loop, then you'll need to add a
> 
> conditional array save for that too.

Thank you for your answer, of course it must save computation time doing a check in the outer loop, rather then create the array every time.

I have another related question i just read there is break in javascript but does it work with while, using your idea could i do something like

newArray = Aarray.slice();

loop1:
while (x > y) {
  if ((x+1) > y) {
      break loop1; 
  }

  while (z > t) {
    change(Aarray);
    z++;
  }
  x++;
} 

The whole package will run many times in a global loop, so there will be many if checks depending on input size of array

Is there no other way then if clause to specify that the inner loop will break earlier last time it is run?

JT
Thanks again.

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Looping holding old copy of array jonas.thornvall@gmail.com - 2013-06-23 22:50 -0700
  Re: Looping holding old copy of array "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2013-06-24 09:44 +0200
    Re: Looping holding old copy of array "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2013-06-24 16:45 -0700
      Re: Looping holding old copy of array Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-06-25 03:06 +0200
        Re: Looping holding old copy of array Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2013-06-25 21:58 +0200
          Re: Looping holding old copy of array Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-06-25 23:03 +0200
  Re: Looping holding old copy of array JJ <duh@nah.meh> - 2013-06-24 15:19 +0700
    Re: Looping holding old copy of array jonas.thornvall@gmail.com - 2013-06-24 03:38 -0700
      Re: Looping holding old copy of array JJ <duh@nah.meh> - 2013-06-25 10:52 +0700

csiph-web