/    Sign up×
Community /Pin to ProfileBookmark

array starting with var[100] ?

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

to post a comment
JavaScript

9 Comments(s)

Copy linkTweet thisAlerts:
@FangSep 23.2003 — Yes, but don't name them [COLOR=green]var[/COLOR].
Copy linkTweet thisAlerts:
@DonMArtinauthorSep 23.2003 — 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
Copy linkTweet thisAlerts:
@pyroSep 23.2003 — 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.
Copy linkTweet thisAlerts:
@DonMArtinauthorSep 23.2003 — 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
Copy linkTweet thisAlerts:
@pyroSep 23.2003 — 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?
Copy linkTweet thisAlerts:
@DonMArtinauthorSep 23.2003 — 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
Copy linkTweet thisAlerts:
@pyroSep 23.2003 — 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>
Copy linkTweet thisAlerts:
@DonMArtinauthorSep 23.2003 — 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
Copy linkTweet thisAlerts:
@DonMArtinauthorSep 24.2003 — 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
×

Success!

Help @DonMArtin spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.24,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...