Closed Thread Icon

Preserved Topic: Arrays Pages that link to <a href="https://ozoneasylum.com/backlink?for=17932" title="Pages that link to Preserved Topic: Arrays" rel="nofollow" >Preserved Topic: Arrays\

 
Author Thread
mobrul
Bipolar (III) Inmate

From:
Insane since: Aug 2000

posted posted 04-18-2001 19:34

I have, in one of my scripts, an array. In the beginning the length of that array is 0. As the user does certain things there are items added to that array. I got all of that.
arr = new Array();
arr_leng = arr.length;

function add(){
arr[parseInt(arr_leng)] = whatever;
}

repeat the above function any number of times as the situation dictates.

Now, how can I take a single item--say, arr[3]-- out of that array so arr_leng = arr_leng - 1...as if arr[3] never was added in the first place?
Does anybody understand my ramblings?
Any help?
This has been driving me nuts, reading through syntax guides and all of the other resources I can get my hands on.

mobrul

kat
Paranoid (IV) Inmate

From: memphis TN
Insane since: Apr 2001

posted posted 04-18-2001 20:53

... i'm not a jscript guru, but my experience w/ php -might- help..


if there's a way to match one thing with another then perform a statement depending on the answer then it should work...

somethin like

function add() {
** php-fied **
for ($i = 0 ; $i < wheneveryouwantittostop ; $i++) {
if ($i == "3") {
break;
} else {
arr[parseInt(arr_leng)] = whatever;
}
}


</script>


if you don't want it to stop.. 'counting', then just omit the 2nd statement and it should look like:
for ($i = 1 ; ; $i++)

{ kat ; mmm.. a site! }

[This message has been edited by kat (edited 04-18-2001).]

kat
Paranoid (IV) Inmate

From: memphis TN
Insane since: Apr 2001

posted posted 04-18-2001 21:01

a friend says this:

function nukeElement(which) {
for ($i=0; $i<array.length; $i++) {
if ($i > which) {
arr[$i-1] = arr[$i];
}
}
arr.length = arr.length-1;
}

{ kat ; mmm.. a site! }

bunchapixels
Neurotic (0) Inmate
Newly admitted
posted posted 04-19-2001 01:11

a few quick properties for you:
lastval = myArray.pop()
this array.pop() returns and removes the last value of the array.
firstval = myArray.shift()
this array.shift() returns and removes the first value of the array, and shifts all the remaining values down one.
myArray.splice(start, deleteCount, value1, value2,...)
this function modifies the array as such:
starting from the array position start, deleteCount number of array items are deleted, including start. then the values value1, value2, .... are then inserted in this area of the array.
if there are no such values entered, the array items are deleted.
if there is only one argument in the function (ie no deleteCount or replacement values), then all values from the array position start to the end of the array are deleted.

eg:
myArray = new Array(1,2,3,4,5,6,7,8)
myArray.splice(2,2,0)
//now the array is: [1,2,0,5,6,7,8]

sound good?

oh, and i dont think that simply modifying array.length actually works...

mobrul
Bipolar (III) Inmate

From:
Insane since: Aug 2000

posted posted 04-19-2001 15:22

Thank you thank you thank you.
I knew about array.pop and array.shift...but that array.splice is something new to me and it sounds like exactly what I need.
Bunchapixels, you rock.

mobrul

Slime
Lunatic (VI) Mad Scientist

From: Massachusetts, USA
Insane since: Mar 2000

posted posted 04-19-2001 17:41

I didn't read too well over the previous posts in here, but... if you want to make an array shorter, all you have to do is set it's length to a shorter value.

// if arr contains ("hi", "there", "how", "are", "you"), and you say this:
arr.length -= 1;
// then arr now contains ("hi", "there", "how", "are").

mobrul
Bipolar (III) Inmate

From:
Insane since: Aug 2000

posted posted 04-19-2001 20:18

True enough, but I may want to take out the 'there' or maybe the 'how' or even the 'are', depending upon the cicumstances.
Looks like .splice is the thing for me.

mobrul

mobrul
Bipolar (III) Inmate

From:
Insane since: Aug 2000

posted posted 04-30-2001 20:56

Alright bunchapixels...I'm expecting you to come to the rescue again...

I tried your "array.splice()"

function destroymemory(y){
//alert(favsmem);
it_name=favsicon[parseInt(y)];
//alert(it_name);
for(i=0;i<favsmem.length;i++){
if(favsmem[i]==it_name){
favsmem.splice(i,1);
}
}
//alert(favsmem);
}

This is cut directly from my code.
'favsmem' is the name of an Array.
'favsicon' is the name of another Array.

Anyway, when I 'un'-comment the alerts...the first one works fine and gives me the entire array as a comma seperated list.
The second alert works fine and accurately gives me 'it_name.'
THE PROBLEM: The third alert isn't even being called and I get an error on screen that says "Object doesn't support this property or method." and that error points directly to the line 'favsmem.splice...'
I haven't been able to find the property 'myArray.splice' in any books.

What am I missing?

mobrul

bunchapixels
Neurotic (0) Inmate
Newly admitted
posted posted 05-01-2001 09:50

hmmm....
get back to you within 24 hours, most likely.
unless of course, mr max, or another asylumnite who actually knows what they are doing beats me to it.
bug, my first bit of advice would be simple error detection: try a splice function on its own, without and 'i' or the for loop.
try it with different values for each - maybe the startval starts from 1, for some inconceivable reason... ill try it on the morrow.

quack http://www.devguru.com/technologies/ecmascript/quickref/splice.html


[This message has been edited by bunchapixels (edited 05-01-2001).]

mr.maX
Maniac (V) Mad Scientist

From: Belgrade, Serbia
Insane since: Sep 2000

posted posted 05-02-2001 06:44

Mobrul, what browser are you using? I've just tried your code in IE5.5 and everything is working fine...

mobrul
Bipolar (III) Inmate

From:
Insane since: Aug 2000

posted posted 05-02-2001 15:54

Also I am using IE 5.5...double s**t!
That means, I guess, that it must be something else...
Maybe I'll have to look deeper. For now I got impatient and wrote a different script...similar to the nukeElement function kat wrote about 8 messages up...it works...but this array.splice is so much 'cleaner.'
...Thanks to all for your help and input.
This new site that I am working on is for an intranet...'hopefully' I will be able to post it in the review section soon. For now it must remain top secret and undercover! <stealthy spy music>

mobrul

« BackwardsOnwards »

Show Forum Drop Down Menu