Click to See Complete Forum and Search --> : array starting with var[100] ?


DonMArtin
09-23-2003, 03:41 PM
I want to store filenames in an array. Those filenames are beginning with numbers, f.e. 100 to 142, 200 to 235 and so on. I want to refer to those filenames with the variable's index. So the numbers should become the index. If I have a list like this:
var[100] = "100file.jpg";
var[213] = "213file.jpg";
...
can I define an array with missing indices?

Thanks

Alex

Fang
09-23-2003, 04:08 PM
Yes, but don't name them var.

DonMArtin
09-23-2003, 04:15 PM
In fact, I didn't name them var, I just wrote that here. Let's say:
variable[100] and variable[200] and so on

Alex

pyro
09-23-2003, 04:34 PM
Note that depending on what you are trying to do, using arrays like this might cause some problems. Take this example:

<script type="text/javascript">
ary = new Array();
ary[2] = "Two";
ary[4] = "Four";
for (i=0; i<ary.length; i++) {
alert (ary[i]);
}
</script>

It will think the array has 5 values (as the last index is 4). In that situation, you could fix the problem by using:

<script type="text/javascript">
ary = new Array();
ary[2] = "Two";
ary[4] = "Four";
for (i in ary) {
alert (ary[i]);
}
</script>But just remember that the array will think it contains more values than it really does.

DonMArtin
09-23-2003, 04:53 PM
I use an external .js-file. I want to define this array and later on refer to those variables in different functions. Where should I place the loop?
Let's say:

variablename[100] = "100file.jpg";
othervariable[100] = "description of 100file.jpg";

Now if I get the 100 sliced out of the URL I want to document.write both variables to the page.
Could I put this slice to the variable pic and then

for (i=pic) {
document.write (variablename[i]);
document.write (othervariable[i]);
}

??

Alex

pyro
09-23-2003, 04:59 PM
Hmm.. I'm not sure what you are asking, but I can tell you that for (i=pic) is not the right syntax. Can you try to re-explain what you need?

DonMArtin
09-23-2003, 05:13 PM
I have thumbnails and enlarged pics in extra folders. When I click on a thumbnail, I want to window.open and document.write to that window the enlarged pic and a text, which belongs to that pic. I want to write the pic-filenames and the referring text on 1(one) page, because I have an English translation too and I don't want to manage too many pages. So the thumnail-pic-filenames are already beginning with unique numbers. The filenames are identical with those of the enlarged pics. With a click on a thmb I pass it's number to the URL and call the window.open. The window takes the number of the URL and looks for the correct enlarged pic and it's referring text. The array has no following numbers because there may be changes, adds or drops.

Of course I could try that in PHP but I thought I could do it without forms.

Are you completely confused now ?

Alex

pyro
09-23-2003, 05:31 PM
If you are passing the number to the page, can't you just use something like this, then?

<script type="text/javascript">
num = window.location.search.substr(1); // gets everything after the ? in the URL
document.write(picvar[num]);
</script>

DonMArtin
09-23-2003, 06:24 PM
that's what I do. If I check it f.e. with alert (num), I get the correct number, but I don't get the picvar[num] because the array seems not to be correctly defined. I do it

var text = new Array (76) //76 items in the array
{
text[100] = "first description";
text[102] = .....and so on
};

it didn't work.

The highest number is 413 and it didn't work with
var text = new Array(413) // and not with 414

Maybe I should try

for (i=0;i<413;i++){
if (i==pic) {
document.write (text[i]);
}
}

Alex

DonMArtin
09-23-2003, 07:37 PM
Thanks for thinking...

that works:

var text = new Array ()
{
text[100] = "fzufghjjk";
text[400] = "jydzfykjhv";
...
};

function gettext(num) {
for (i=0;i<401;i++){
if (i==num){
description = text[num];
return (description);
}
}
}

...always learning...

Alex