Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Oct 11th, 2007, 4:45 AM   #1
dpsleep
Newbie
 
Join Date: May 2006
Posts: 16
Rep Power: 0 dpsleep is on a distinguished road
compile a var name from another var

i have a script that holds the functions for a desklet. the desklet is to be ran in a program called avedesk. ultimately this block of code is meant to first check for the next side to flip to, then check if that side is enabled, then switch to it if it is, if its not, goto the next side and check it.

the variables of chk0-chk4 are ether true or false, true meaning the side is enabled false meaning it isn't and to skip it.

side is the current active side of the desklet

the newside stuff is just telling it if the current side is not the last side add one, else goto the first side.

then we got the while loop

now the problem im having is that im just starting js as this desklet program requires it. so i don't know much about the syntax and have been trying to get by on frequent Google searches.

the part im having trouble with is
if(newside == i && ("chk" + i) == 'true'){

what im trying to do is have the var i from the loop be added after chk so that if i == 2 it work read out to:
if(newside == i && ("ch" + i) == 'true'){
if(newside == i && chk2 == 'true'){

this is the best way i could come up with to get the reaction i want, i just don't know how to make the two add together.

function flipnext(){
	var chk0 = this.parameters('radarchk1');
	var chk1 = this.parameters('radarchk2');
	var chk2 = this.parameters('radarchk3');
	var chk3 = this.parameters('radarchk4');
	var chk4 = this.parameters('radarchk5');
	side = desklet.GetcurrentSide();
	if(side < 4){
		newside = side + 1
	}else{
		newside = 1
	}
	i = 0
	while(i < 5){
		if(newside == i && ("chk" + i) == 'true'){
			desklet.FlipTo(i);
			alert(i);
		}else if(("chk" + i) == 'false'){
			newside + 1
		}
	i++
	}
}

sorry if this has been solved before, i didn't know what to call it so i didn't really know what to search for. any help on this method or maybe an alternative method would be greatly appreciated. thanks.
dpsleep is offline   Reply With Quote
Old Oct 12th, 2007, 8:03 PM   #2
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 816
Rep Power: 4 The Dark is on a distinguished road
try
eval("chk" + i) == 'true'
The Dark is offline   Reply With Quote
Old Oct 13th, 2007, 3:53 AM   #3
dpsleep
Newbie
 
Join Date: May 2006
Posts: 16
Rep Power: 0 dpsleep is on a distinguished road
Quote:
Originally Posted by The Dark View Post
try
eval("chk" + i) == 'true'
thanks, works like a charm.
dpsleep is offline   Reply With Quote
Old Oct 17th, 2007, 7:37 PM   #4
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 816
Rep Power: 4 The Dark is on a distinguished road
Re: compile a var name from another var

One of the posts that went missing in this thread (I can't remember who wrote it) pointed to this blog: Eval is Evil
I read through that and have made some modifications to some of my code that was using eval extensively to test for the existence of a list of variables. It now runs in 1/5th of the time! It was actually slowing the whole program down quite a bit, as this list has grown slowly from about 20 variables to over 500.

So thanks to whoever it was who posted it previously.
The Dark is offline   Reply With Quote
Old Oct 18th, 2007, 10:37 AM   #5
~s.o.s~
Megalomaniac
 
~s.o.s~'s Avatar
 
Join Date: Nov 2006
Posts: 8
Rep Power: 0 ~s.o.s~ is on a distinguished road
Re: compile a var name from another var

> So thanks to whoever it was who posted it previously.
Yeah no problems, I was as much surprised as you are regarding the missing post. ;-)

Just to make matters more clear, every variable you create is a property of some object or the another. In cases where you don't have explicit namespaces declared or trying to declare a function, the parent object is the 'window' object. It is easier to remember this if you keep in mind that almost everything in Javascript is a hash with key value pairs (even arrays!).
~s.o.s~ is offline   Reply With Quote
Old Oct 18th, 2007, 3:07 PM   #6
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Re: compile a var name from another var

Quote:
...every variable you create is a property of some object or the another...
While this is true of some languages (not all), it is definitely not true of the underlying microprocessor, as a whole.

Therein lies the problem. Abstraction is a moving target. To define something as "evil", whether it be an eval or a goto, is to indulge in the kind of behavior that judges a food by the taste of the wrapper.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Oct 19th, 2007, 10:52 AM   #7
~s.o.s~
Megalomaniac
 
~s.o.s~'s Avatar
 
Join Date: Nov 2006
Posts: 8
Rep Power: 0 ~s.o.s~ is on a distinguished road
Re: compile a var name from another var

Quote:
Originally Posted by DaWei View Post
While this is true of some languages (not all), it is definitely not true of the underlying microprocessor, as a whole.
Yes I know, and this being 'Javascript forum', I was specifically referring to browser embedded javascript.

Quote:
Therein lies the problem. Abstraction is a moving target. To define something as "evil", whether it be an eval or a goto, is to indulge in the kind of behavior that judges a food by the taste of the wrapper.
Judging the content without reading the context is to indulge in the kind of behavior that judges a food by the taste of the wrapper. The 'evil' there is used to emphasize how frequently eval is misused and misunderstood by programmers.
~s.o.s~ is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
please help me to compile cwl157 C++ 37 Mar 6th, 2006 6:12 PM
MASM32 compile problem Kilo Assembly 3 Nov 20th, 2005 12:39 PM
can't compile code lloydkirk Java 17 Oct 6th, 2005 2:07 AM
Fahrenheit / Celsius compile problem andrewgray C++ 20 Jul 8th, 2005 11:04 PM
C++ Compile gebreil C++ 6 May 29th, 2005 1:30 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 6:36 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC