/    Sign up×
Community /Pin to ProfileBookmark

3X106 array syntax help make my sudo into code

I have a very large script and I need to connect a few pieces of java script that I have figured out to put it all together. So here’s my sudo code with comments about what I want to do. if you can change any of it into actual code it would be great. Thanks in advance!

[code=php]//make a new array by hand not with a loop that contains 106 or so elements with each element containing three associated values here I just want to know how to make the array please do not waste your time writing 106 entry’s I can do that

new array veglookup()
{
veglookup[0] (name=”apple”, element=39 , vegetable=”NO”)
veglookup[1] (name=”banana”, element=42 , vegetable=”NO”)
veglookup[2] (name=”ketchup”, element=40 , vegetable=”YES”)
.//more elements here
.//and yet more
veglookup[106] (name=”my brain”, element=150 , vegetable=”MABEY”)
}

//choose a random element from the array
var rande= Math.floor(Math.random() * veglookup.legnth);

//retrieve the value of element to use in some document.form.element[i] statements later
var i= veglookup[rande].element.value

//retrieve the name to use to set new values for vegtable based on other stuff going on inside the if statement
var vegname=veglookup[rande].name.value
if (vegname==”my brain”)
{

//set values within the array
veglookup[rande].vegtable.value=”YES”
}

//get the info from a if and or not exc.
if (veglookup[rande].vegtable.value==”YES”)
{
alert(” “+vegname+” is a vegetable!”)

//use the element #in a document.form.element[i]statement
document.form.element[i].value=((document.form.element[i].value)*1)-1
}
[/code]

Thank’s again!

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@sciguyryanAug 14.2004 — I presume in lines like these:


veglookup[0] (name="apple", element=39 , vegetable="NO")

veglookup[1] (name="banana", element=42 , vegetable="NO")

veglookup[2] (name="ketchup", element=40 , vegetable="YES")
[/quote]


You are trying to make an array within an array?

If so, in Javascript this is not possible directly.
Copy linkTweet thisAlerts:
@Mr_JAug 14.2004 — Something like this maybe

<script>

veglookup=new Array() // name element value

veglookup[0]=new Array("apple",39,"NO")

veglookup[1]=new Array("banana",42,"NO")

veglookup[2]=new Array("ketchup",40,"YES")

veglookup[4]=new Array("my brain",150,"MAYBE")


//choose a random element from the array

var rande= Math.floor(Math.random() * veglookup.length);

//retrieve the value of element to use in some document.form.element[i] statements later

var el= veglookup[rande][2]



//retrieve the name to use to set new values for vegetable based on other stuff going on inside the if statement

var vegname=veglookup[rande][0]

if (vegname=="my brain"){

//set values within the array

veglookup[rande][2]="YES"

}



//get the info from a if and or not exc.



for(i=0;i<veglookup.length-1;i++){

if (veglookup[rande][2]=="YES"){

alert(" "+vegname+" is a vegetable!")

}

}



//use the element #in a document.form.element[i]statement

//document.form.element[i].value=veglookup[rande][1]





</script>
Copy linkTweet thisAlerts:
@ylbissopauthorAug 14.2004 — Thank you oh Supreme Master of the Web!!

there's no need to awnser this but just out of curiosity

when you declare

veglookup[0]=new Array("apple",39,"NO")

are you declaring an array within an array? and if so can you declare

veglookup[0]=new Array(subarray,subarray2)

subarray[0]=new Array("string",42,whatever)

subarray2[0]=new Array("string",42,whatever)

and then get

var vegname=veglookup[0][subarray][0]

again thanks!
Copy linkTweet thisAlerts:
@sciguyryanAug 14.2004 — [i]Originally posted by ylbissop [/i]

[B]Thank you oh Supreme Master of the Web!!



there's no need to awnser this but just out of curiosity



when you declare



veglookup[0]=new Array("apple",39,"NO")



are you declaring an array within an array? and if so can you declare



veglookup[0]=new Array(subarray,subarray2)

subarray[0]=new Array("string",42,whatever)

subarray2[0]=new Array("string",42,whatever)



and then get

var vegname=veglookup[0][subarray][0]



again thanks! [/B]
[/QUOTE]



Yes, that is declaring an array within an array, a setout somthing like:


veglookup // Main variable

- 0 // Array Level 1

- apple // Array Level 2

- 39 // Array Level 2

- NO // Array Level 2



Hope that helps,

RyanJ
Copy linkTweet thisAlerts:
@Mr_JAug 14.2004 — 
when you declare

veglookup[0]=new Array("apple",39,"NO")

are you declaring an array within an array?
[/QUOTE]


Yes



and if so can you declare

veglookup[0]=new Array(subarray,subarray2)

subarray[0]=new Array("string",42,whatever)

subarray2[0]=new Array("string",42,whatever)

and then get

var vegname=veglookup[0][subarray][0]

[/QUOTE]


In a way yes, although your the syntax is not quite right


var main = new Array()

main[0]=new Array(1,2,3,4,5)

main[1]=new Array(6,7,8,9,10)

// the above would be referenced as [B]main[indexnum][indexnum][/B]

main[2]= new Array(

new Array("2 - 2 - 0"),

new Array("2 - 2 - 0","2 - 2 - 1"),

new Array("2 - 2 - 0","2 - 2 - 1","2 - 2 - 2")

),

// the above would be referenced as [B]main[indexnum][indexnum][indexnum][/B]
×

Success!

Help @ylbissop 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.5,
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: @Yussuf4331,
tipped: article
amount: 1000 SATS,

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

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...