/    Sign up×
Community /Pin to ProfileBookmark

Indexing an array with an array

I cannot for the life of me figure out why my incrementation of an Array coordinate will not increment just a single value. This is a stripped down version of the entire document, but all of the info is here that you need to evaluate. This an ASP application. I am passing VBScript variable values to javascript for user manipulation.

<head>
<script language=”JavaScript” type=”text/javascript”>
//create var for debugging
var debug = 1;
/*create array for holding coordinates to hold a value representative
of a grid of values to use in displaying different image states manipulated by user-click.*
/
var pegIncrement = new Array(10,4);
for (i=0;i<10;i++){
for (j=0;j<4;j++){
pegIncrement[i,j] = 0;
}
}
/*create a function for grabbing the name of the image placeholder and changing it’s source to another value*/
function changePeg(pegPosition){
//find the name of the image and split the x,y values

incrementCords = pegPosition.split(“|”)
//condition for starting back over after 6 increments
if (pegIncrement[incrementCords[1],incrementCords[2]] == 7){
pegIncrement[incrementCords[1],incrementCords[2]] = 0;
}
/*change the .src of the image to the array() value representatvie of a relative image path references which are not present in this script*/
document.images[pegPosition].src = arJSPegColors[pegIncrement[incrementCords[1],incrementCords[2]]];
//increment the array value-this is where the problem is
pegIncrement[incrementCords[1],incrementCords[2]]++
//the following is for testing
if (debug == 1){
writeCords = “”
for (i=0;i<10;i++){
for (j=0;j<4;j++){
writeCords += pegIncrement[i,j]
if (j!=3){
writeCords += ” | “;
}
}
writeCords += ‘n’;
}
document.test.cords.value = writeCords;
}
}
</script>
</head>
<body>
<%
Response.Write(“<img src=’empty.gif’ name=’img|” & j & “|” & k & “‘ onClick=’changePeg(this.name);’>”)
%>
<br>
<form name=”test”>
<textarea name=”cords” cols=”20″ rows=”15″></textarea>
</form>

The textarea maps out the contents of the pegIncrement Array(). This is what is displayed when any one image is clicked in the second column of images…

0 | 1 | 0 | 0
0 | 1 | 0 | 0
0 | 1 | 0 | 0
0 | 1 | 0 | 0
0 | 1 | 0 | 0
0 | 1 | 0 | 0
0 | 1 | 0 | 0
0 | 1 | 0 | 0
0 | 1 | 0 | 0
0 | 1 | 0 | 0

It is changing the value across the entire column insted of just on ordinate. It should look like this…

0 | 0 | 0 | 0
0 | 0 | 0 | 0
0 | 0 | 0 | 0
0 | 0 | 0 | 0
0 | 0 | 0 | 0
0 | 1 | 0 | 0
0 | 0 | 0 | 0
0 | 0 | 0 | 0
0 | 0 | 0 | 0
0 | 0 | 0 | 0

each value should be independant of one another. I think it’s here where the problem is…

pegIncrement[incrementCords[1],incrementCords[2]]++

I just don’t understand why incrementing one value would loop and increment the entire column.

Anyone have any suggestions? Need more info? Thanks, Y’all

chromahoen

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@TageMay 30.2004 — I would like to see what the name of the image would be. (the resulting html code of the img) I might be able to help then.

Edit: Nevermind, I think I know what they are now, rows and columns... Sorry for my slow brain, lol.

Tage
Copy linkTweet thisAlerts:
@TageMay 30.2004 — Hello! That line is not the problem. You are declaring two-dimensional arrays incorrectly, you use two []'s, first one is the first 10 arrays, the second one is for the 4 sub-arrays for each 10 arrays. You also have to declare it differently. Your script should look something like in the attached file... (sorry, I had to make it look more readable so I could find the problem) Hope this helps...

Tage

P.S. [url=http://www.javascriptkit.com/javatutors/twoarray1.shtml]Good tutorial on two-dimensional arrays[/url]

[upl-file uuid=4a2ce4bf-6eb4-43b6-a7a1-17f38d952a0e size=2kB]arraytest.txt[/upl-file]
Copy linkTweet thisAlerts:
@chromahoenauthorMay 30.2004 — Tage,

Thank you very much for the follow-up. You were correct. Thank you for the Tut.

I think I am most surprised that I was able to get anywhere with the way I had it. I never got an error, and the debug contents were at least displaying the zeros and the digit all the way down the column when clicked. Wouldn't you think since I had ,'s in there in stead of []'s that it would error out?

Anyway thank you for the heads up. That's what happens when you learn VBScript and JavaScript at the same time.

Thanks,

chromahoen
×

Success!

Help @chromahoen 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.18,
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,
)...