Closed Thread Icon

Preserved Topic: An array with more than one variable? (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=18022" title="Pages that link to Preserved Topic: An array with more than one variable? (Page 1 of 1)" rel="nofollow" >Preserved Topic: An array with more than one variable? <span class="small">(Page 1 of 1)</span>\

 
Pugzly
Paranoid (IV) Inmate

From: 127.0.0.1
Insane since: Apr 2000

posted posted 06-12-2001 17:31

Okay, it's time for my question of the day

I have written a script that involves several arrays - one that lists some images, and another that contains captions for those images. If I delete one entry in one array without deleting the appropriate item in the other, then all hell breaks lose (as it should). So this got me thinking. Is is possible to create a single array that contains both?



[This message has been edited by Pugzly (edited 06-12-2001).]

linear
Paranoid (IV) Inmate

From: other places
Insane since: Mar 2001

posted posted 06-12-2001 18:26

Yes, but....

What you want is an array of objects. The object should have a property corresponding to each of the arrays you were tracking before. Here's a sample from the planetary locator:

// constructor for the inmate object
function inmate(name, latitude, longitude, email, url) {
this.name = name;
this.latitude = latitude;
this.longitude = longitude;
this.email = email;
this.url = url;
this.popup = name + 'pop';
this.popup2 = name + 'pop2';
// where is this inmate on our map?
this.X = longitude2x(longitude, width) - 4;
this.Y = latitude2y(latitude, height) - 3;
}

function initialize() {
// we have an array of inmate objects. this
// function loops throught them and create() handles
// creating and positioning all the DIVs that
// belong to each inmate.
for (i = 0; i < inmates.length; i++) {
create (inmates[i]);
}
}

inmates[0] = new inmate('linear', 38.967, -94.667, 'linear@linear1.org', 'http://linear1.org/');
inmates[1] = new inmate('Allewyn', 42.44, -123.17, 'davidh@itilink.com', 'http://www.allewyn.f2s.com/');

and so on...

Notice that my function is called inmate() and I instantiate an inmate object by the assignment
inmates[] = new inmate(<arguments here> ). That's all there is to it. This is a much more readable way to do it, even though you can mix types in an array, AFAIK.

Pugzly
Paranoid (IV) Inmate

From: 127.0.0.1
Insane since: Apr 2000

posted posted 06-12-2001 20:27

Okay, I'm running into a little trouble. Here's my code:

<script>
// constructor for the inmate object
function inmate(name, email, url) {
this.name = name;
this.email = email;
this.url = url;
}

function initialize() {
for (i = 0; i < inmates.length; i++) {
create (inmates[i]);
}
}

inmates = new Array();
inmates[0] = new inmate('Pugzly', 'pugzly@runningwolf.com', 'http://www.pugzly.com/');
inmates[1] = new inmate('Sniper', 'sniper@runningwolf.com', 'http://www.runningwolf.com/');
</script>

<body onload=initialize();>
<script>
document.write("There are " + inmates.length + " items in the array<br>");
for (x = 0; x <inmates.length; x++){
document.write((x+1) + "/" + inmates.length + " " + inmate.name + "<br>");
}
</script>

I'm getting OBJECT EXPECTED in line 11 (the create (inmates[i]) line). So this is causing my array to not get created (I think). But I'm at a loss as to the reason this is happening. My output ends up being:


There are 2 items in the array
1/2 undefined
2/2 undefined


[This message has been edited by Pugzly (edited 06-12-2001).]

mr.maX
Maniac (V) Mad Scientist

From: Belgrade, Serbia
Insane since: Sep 2000

posted posted 06-12-2001 20:55

Pugzly, you don't need initialize() function at all (you also don't have the create() function defined, and that's the error that's being reported). Anyway, besides that, you should change the code that's inside <body></body> to read (difference is bolded):

<script>
document.write("There are " + inmates.length + " items in the array<br>");
for (x = 0; x <inmates.length; x++){
document.write((x+1) + "/" + inmates.length + " " + inmates[x].name + "<br>");
}
</script>

Pugzly
Paranoid (IV) Inmate

From: 127.0.0.1
Insane since: Apr 2000

posted posted 06-12-2001 21:23

Ah...okay. I wasn't sure what that was doing. As soon as I took the onload statement out, the error went away. Your syntax correction got the script running.

Very cool. This is much easier than my previous version of multiple arrays.

Thanks to all!

Pugzly
Paranoid (IV) Inmate

From: 127.0.0.1
Insane since: Apr 2000

posted posted 06-13-2001 04:36

Okay. This is working all nice and fancy and stuff. My only other question is this:

My array is laid out like:

slides = new Array();
slides[0] = new slide('27', 'Our new SWAT tank');
slides[1] = new slide('43', 'The G36 machine gun');
blah blah blah


This goes on for about 165 items. So, removing slides[93] because it's not longer needed means I have to renumber all the array items after that, right? Isn't there an easier way to create the array without having to manually number each item?


linear
Paranoid (IV) Inmate

From: other places
Insane since: Mar 2001

posted posted 06-13-2001 05:12

I think you're grasping at array.splice and/or array.slice

In PHP you can say slides[] = foo and the subscript is handled magically, but AFAIK that don't work in JS. I guess push is pretty much that.



Edit: sorry to have misled you with the create() business. I really just wanted to show that you could iterate over the array of objects. I should have just omitted that snippet.

[This message has been edited by linear (edited 06-13-2001).]

Pugzly
Paranoid (IV) Inmate

From: 127.0.0.1
Insane since: Apr 2000

posted posted 06-13-2001 18:21

Not a problem on the error - it tested my troubleshooting skils (which failed me).

Hmmm...slice....splice....push.....

All cool, but I'm looking at more hard coded issues. Thanks!

linear
Paranoid (IV) Inmate

From: other places
Insane since: Mar 2001

posted posted 06-13-2001 18:39



You should be able to write a user function that cleanly yanks out an element from the middle of your array using splice. That's what I was getting at.

But you're talking about in the code where you populate each array element, I guess. You could push new objects on to the array in place of declaring them with an explicit subscript. That's a good as it can get without a server-side database query, I reckon.

Pugzly
Paranoid (IV) Inmate

From: 127.0.0.1
Insane since: Apr 2000

posted posted 07-07-2001 06:35

Okay....hmmm...

I have this one script that looks like this:

function initArray() {
this.length = initArray.arguments.length;
for (n=0; n<initArray.arguments.length; n++)
this[n] = initArray.arguments[n];
return this;
}

captions = new initArray(
"blah blah blah",
"more blah blah blah",
);


And this creates my array of captions, which I tie into a function that attaches a caption to a picture. This works fine. Is there a way to do something like this:

images = new initArray(
'image1.jpg','this is my caption'
'image2.jpg','this is my other caption'
);

And just have the javascript create an array and grab the image name and the caption, without having to precede it with something like images[0] = new image? Does this make sense?

I also thought about adding another variable to the array, such as 'display', and then, when running the function that builds the array, only adding the items if the 'display' is true. I suppose this would be easier, right?

Pugzly
Paranoid (IV) Inmate

From: 127.0.0.1
Insane since: Apr 2000

posted posted 07-07-2001 08:17

Ok, after playing with this for a while, it seems SPLICE is the way to go (I know, I'm a little sloooooow)....

Anyways, I can get it to work, for the most part. If I set an array item to FALSE, it does correctly yank it out. All is fine, as I can do this to a whole bunch of items in the array. The problem I'm having is that if I set two CONSECUTIVE items to false, I get an incorrect array back. I have a test page at splice. On that page, I set the 2nd, 6th, and 8th items to false, and the array comes back three items shorter (like it should). Now, if I also set the 1st item (which, of course, is adjacent to the 2nd item) to false, I get a problem, as shown in broken splice.



[This message has been edited by Pugzly (edited 07-07-2001).]

Pugzly
Paranoid (IV) Inmate

From: 127.0.0.1
Insane since: Apr 2000

posted posted 07-07-2001 08:28

And, now, in the wee hours, something hit me (and it wasn't my wife). i figured that when an item is removed the counter is still getting increased one, meaning the adjacent item wasn't getting checked. So, I changed

function spliceMe(){
for (x = 0; x <images.length; x++){
if (images[x].display != "true"){
images.splice(x, 1);
}
}
}

to

function spliceMe(){
for (x = 0; x <images.length; x++){
if (images[x].display != "true"){
images.splice(x, 1);
x--;
}
}
}

and it started to work fine. I think I'm finally getting the hang of this.....

« BackwardsOnwards »

Show Forum Drop Down Menu