Avoiding EVAL

G

Guest

I am using one of these two dimensional arrays

mun=new Array()

mun[1] = new detail("58","89","896")
mun[2] = new detail("54","25","457")

function detail(ara,grp,nme){
this.ara = ara
this.grp = grp
this.nme = nme
}


from the above we can see that the followinng line

mun[2].grp

will return "25"


As there are several tables with multiple arrays I need to refer to
these through other variables
I can do it by using the eval function as in in the following few
lines.

var rng = 'mun'
var iv = '2'
eval(rng + '[' + iv + '].grp'))

how can I achieve the same without the "eval" function.

I have tried the following but this just returns a string

temp = mun + '[' + iv + ']'
alert (temp.grp)
returns "undefined"

and

temp = mun + '[' + iv + '].grp'
alert (temp)
returns the string "mun[2].grp"
 
T

The Natural Philosopher

I am using one of these two dimensional arrays

mun=new Array()

mun[1] = new detail("58","89","896")
mun[2] = new detail("54","25","457")

function detail(ara,grp,nme){
this.ara = ara
this.grp = grp
this.nme = nme
}


from the above we can see that the followinng line

mun[2].grp

will return "25"


As there are several tables with multiple arrays I need to refer to
these through other variables
I can do it by using the eval function as in in the following few
lines.

var rng = 'mun'
var iv = '2'
eval(rng + '[' + iv + '].grp'))

how can I achieve the same without the "eval" function.

I have tried the following but this just returns a string

temp = mun + '[' + iv + ']'
alert (temp.grp)
returns "undefined"

and

temp = mun + '[' + iv + '].grp'
alert (temp)
returns the string "mun[2].grp"

why not an array of arrays

so you an use e.g. myarray['mun'][iv];
 
G

Guest

I am using one of these two dimensional arrays
mun=new Array()
mun[1] = new detail("58","89","896")
mun[2] = new detail("54","25","457")
function detail(ara,grp,nme){
   this.ara = ara
   this.grp = grp
   this.nme = nme
}
from the above we can see that the followinng line
mun[2].grp

will return "25"
As there are several tables with multiple arrays I need to refer to
these through other variables
I can do it by using the eval function as in in the following few
lines.
var rng = 'mun'
var iv = '2'
eval(rng + '[' + iv + '].grp'))
how can I achieve the same without the "eval" function.
I have tried the following but this just returns a string
temp = mun + '[' + iv + ']'
alert (temp.grp)
returns "undefined"

temp = mun + '[' + iv + '].grp'
alert (temp)
returns the string "mun[2].grp"

why not an array of arrays

so you an use e.g. myarray['mun'][iv];- Hide quoted text -

- Show quoted text -

I am already using "array of arrays" or as i would refer to them as
"two dimensional" arrays. The problem that I am having, is that I am
unable to extract data from those arrays without resorting to the
"EVAL" function.

as already stated

eval(rng + '[' + iv + '].grp'))

works perfectly
 
J

Jeremy J Starcher

I am using one of these two dimensional arrays

Actually, you are using an array of objects. The difference is important.

mun=new Array()

Better to use an array literal:
mun = [];
mun[1] = new detail("58","89","896")
mun[2] = new detail("54","25","457")

(What about mun[0]? In Javascript, Arrays are zero-based.)

It is common practice to give constructors an Uppercased name
function Detail(ara, grp, nme)
function detail(ara,grp,nme){
this.ara = ara
this.grp = grp
this.nme = nme
}


from the above we can see that the followinng line

mun[2].grp

will return "25"
Indeed.



As there are several tables with multiple arrays I need to refer to
these through other variables
I can do it by using the eval function as in in the following few lines.

var rng = 'mun'
var iv = '2'
eval(rng + '[' + iv + '].grp'))


Why are you making iv a string? Array indexes are NUMERIC. Integer, to
be more precise.

Why are you making rng a string instead of a reference?

Try:

var rng = mun; // rng and mun now point to the same thing.
var iv = 2; // Integer now
alert(rng[iv]);
 
G

Guest

I am using one of these two dimensional arrays

mun=new Array()

Better to use an array literal:
mun = [];

have never tried that, does it do the same thing?

mun[1] = new detail("58","89","896")
mun[2] = new detail("54","25","457")

(What about mun[0]?  In Javascript, Arrays are zero-based.)

Actually there are 3 arrays
cor[0] --> cor[218]
mun[0] --> mun[283]
gra[0] --> gra[223]

It is common practice to give constructors an Uppercased name

i would thought that was a matter of style (even if poor style) and
should not make any differance

Why are you making iv a string?  Array indexes are NUMERIC.  Integer,to
be more precise.
OK, have tried parseInt(iv,10) and have for test purposes substituted
iv for a number
sadly no luck:)
Why are you making rng a string instead of a reference?

Try:

var rng = mun;  // rng and mun now point to the same thing.
var iv = 2;     // Integer now
alert(rng[iv]);

when i do this it just returns a very long list of (objectObject)


have eventually settled for
var kkk = eval(rng + '[' + iv + ']'))

then i can use
kkk.nme
kkk.grp

etc
etc

So at least the eval function will only be used once.

see
http://www.corbetteer.co.uk/munros/map/newmap.php
 
D

David Mark

Better to use an array literal:
mun = [];

have never tried that, does it do the same thing?
mun[1] = new detail("58","89","896")
mun[2] = new detail("54","25","457")
(What about mun[0]?  In Javascript, Arrays are zero-based.)

Actually there are 3 arrays
cor[0] --> cor[218]
mun[0] --> mun[283]
gra[0] --> gra[223]
It is common practice to give constructors an Uppercased name

i would thought that was a matter of style (even if poor style) and
should not make any differance
Why are you making iv a string?  Array indexes are NUMERIC.  Integer, to
be more precise.

OK, have tried parseInt(iv,10) and have for test purposes substituted
iv for a number
sadly no luck:)
:(
Why are you making rng a string instead of a reference?

var rng = mun;  // rng and mun now point to the same thing.
var iv = 2;     // Integer now
alert(rng[iv]);

when i do this it just returns a very long list of (objectObject)

have eventually settled for
var kkk = eval(rng + '[' + iv + ']'))

Which is crazy.
then i can use
kkk.nme
kkk.grp

etc
etc

So at least the eval function will only be used once.

Think of eval as a hand grenade.

From your original post:

mun=new Array()

mun[1] = new detail("58","89","896")
mun[2] = new detail("54","25","457")

function detail(ara,grp,nme){
this.ara = ara
this.grp = grp
this.nme = nme

}

You pointed out that mun[2].grp == '25'. And you pointed out that you
could eval a string with the same result. What exactly is the
problem?


[snip]
 
G

Guest

As there are several tables with multiple arrays I need to refer to
these through other variables
I can do it by using the eval function as in in the following few
lines.
var rng = 'mun'
var iv = '2'
eval(rng + '[' + iv + '].grp'))
how can I achieve the same without the "eval" function.
I have tried the following but this just returns a string
temp = mun + '[' + iv + ']'

Did you try temp = mun[iv]?

If you need to decide which of the three arrays (mun, cor, gra) you want
to access, you could do it like this:

var allArrays = { mun: mun, cor: cor, gra: gra };

var arrName = "cor";
var arrIndex = 42;
var value = allArrays[arrName][arrIndex].grp;
// this will set value to cor[42].grp

  - Conrad

Many thanks Conrad

works perfectly and I can now remove the eval function.

went for

var allArrays = { munro: munro, corbett: corbett};
var kkk = allArrays[rng][parseInt(rcHill.slice(1),10)];

then i can use the following.
kkk.ara
kkk.grp

etc etc

to be quite honest I do not really understand the line
var allArrays = { munro: munro, corbett: corbett};
 
L

Lasse Reichstein Nielsen

The Natural Philosopher said:
I am using one of these two dimensional arrays
mun=new Array()
mun[1] = new detail("58","89","896")
mun[2] = new detail("54","25","457")

or
var mun = [ , // why not start at zero?
new detail("58","89","896"),
new detail("54","25","457") ];
function detail(ara,grp,nme){
this.ara = ara
this.grp = grp
this.nme = nme
}
from the above we can see that the followinng line
mun[2].grp
will return "25"
Yes.
As there are several tables with multiple arrays I need to refer to
these through other variables

So, let's say you have variables "mun", "bar", "foo" and "baz", all
referring to "two-dimensional arrays"
I can do it by using the eval function as in in the following few
lines.
var rng = 'mun'
var iv = '2'
eval(rng + '[' + iv + '].grp'))

Or
var tmp = mun;
var iv = 2;
tmp[iv].grp; // and do something with it.

The big problem (or one of them) with eval is that it mixes
program and data. A clear separation between the two means that
programming errors are more easily detected, instead of turning
up as runtime-errors.

So, don't ever put the name of program level constructs (like
variables and types) into data. I.e., "var rng = 'mun';" is
a bad thing to do. This is where you should start the change
in your program. "Needing" to use eval is just a symptom of
having already mixed program and data.

So, of you want to abstract over your arrays, i.e., have a variable
that referes to one of the four arrays, you should do just that:
refer to the array (data), not to the variable that happens to point
to it (program).

So,
var rng = mun;
or, more likely:
var rng;
if (someCondition) {
rng = mun;
} else {
rng = foo;
}
I have tried the following but this just returns a string
temp = mun + '[' + iv + ']'

Writing program code: Bad!
Manipulate (references to) data:
var temp = mun[iv];
alert (temp.grp)
returns "undefined"
and
temp = mun + '[' + iv + '].grp'
alert (temp)
returns the string "mun[2].grp"

Yes, that is what you are asking for.
why not an array of arrays
so you an use e.g. myarray['mun'][iv];

That's another way to go. If "mun" is a data level name (e.g., a name
selected from a list by the user), then you should have a data-level
name lookup, typically an object:
var mydata = {
mun: [ , // why not start at zero?
new detail("58","89","896"),
new detail("54","25","457") ],
foo: [ , // ...
]
}


/L
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top