473,388 Members | 1,177 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

why does shift stop my loop over this array

Hi,
I have an array like the following:

for(x=0;x<results.length;x++){

alert(results.length);
extracted=results.shift();

alert(results.length);
if(results.indexOf(extracted)== -1){

alert(extracted + "was 1")
}else{
alert(extracted + "was more than 1");
alert("here" + results.toString());
alert(results.length);
}
}

with a bunch of results in there. I loop over the results, starting at
8, outputting the number before the shift and the number after the
shift, when I get to the number after the shift = 4 then the indexOf
extracted in the array is obviously not -1 and I get the else, which
tells me that the index of my searchstring was more than 1, and that
the length of my results are 4. Then it stops looping.

This happens when I get to the first instance of a value that is
repeated in the array.

If I do the following

for(x=0;x<=results.length;x++){

alert(results.length);
extracted=results.shift();

alert(results.length);
alert(results.toString());
}

then the same thing happens. the last results toString() I get is

Sonia, me (2),Sonia, me (2),Sonia, me (2),Sonia, me (2)

each Sonia, me (2) is an individual item.

Thanks

Sep 10 '07 #1
1 2052
On Sep 10, 9:18 am, pantagruel <rasmussen.br...@gmail.comwrote:
>
If I do the following

for(x=0;x<=results.length;x++){

alert(results.length);
extracted=results.shift();

alert(results.length);
alert(results.toString());

}
Commit this to memory as if it was from
Gargantua:

When writing an iterative loop on the
members of an array, NEVER call a mutator
on that array inside the loop or your code
will behave like flies on a dung heap,
erratic and buzzing all over the place.

[had to stick in a scatalogical ref. :-)]

You forgot to put in an alert for x.
That will show you why it's not working
the way you expect it to.

Try this instead...

while(results.length>0){

alert(results.length);
extracted=results.shift();

alert(results.length);
alert(results.toString());

}

---
Geoff

Sep 10 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: lawrence | last post by:
Suppose I create dynamic web pages with 3 functions (which call other functions to make everything happen, but these 3 you might think of as being the top layer): registerSessions();...
3
by: csx | last post by:
Hi all, Ive got a problem with recursion in Javascript. For this tree: http://www.pcm.uklinux.net/structure.jpg If you input node 3 (i.e. C) which is represented as 'values' in the array, it...
1
by: Chris | last post by:
How do I stop displaying "undefined" when using the code below. The problem occurs as I want to display 5 items per page using setInterval but this causes 'mycars' to reach a value that doesn't...
11
by: Kenneth Lantrip | last post by:
Anyone got any ideas as to how this process could be improved for speed? this is what I have... Dim j, q As Integer Dim x(16), y(16) As Byte x.CopyTo(y, 0) ' shift left circular 24 bits
5
by: Matt Kowalczyk | last post by:
How come this function doesn't work? void shift(char** list, int index) { char buff = (*list); memmove((*list)+1, *list, index); (*list) = buff; return; } I call it like so:
12
by: Mick_fae_Glesga | last post by:
OK, the solution to this is probably blindingly obvious to everyone, but... surely it can't be right. I am compiling with borland bcc32 free compiler this piece of code is designed to identify...
26
by: Shraddha | last post by:
I got a small programm on net...but there was different initialisation that I saw... It was as follows: for ( i = ~0 ; i ; i>>=1); right shift is ok..But what is meaned by " i = ~0 "...
5
by: =?Utf-8?B?Sm9uYXRoYW4gU21pdGg=?= | last post by:
I have written a service, but it won't stop, Eventvwr reports the following: "Failed to stop service" The code is as follows: ============================================ Protected Overrides...
2
by: Bint | last post by:
Hi, What is a simple way to shift the elements in an array, circularly? Is there a way to do it so that you don't need a separate storage array? IE I want to shift array A{1,2,3,4,5,6,7,8} by...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.