By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,599 Members | 1,872 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,599 IT Pros & Developers. It's quick & easy.

a very simple question?!

P: n/a
Hello,

No doubt this is simple but I cannot see how to do it ...

I have a variable called situation_number with a series of values 1,
2, 3 etc.

I would like to have a series of variables situation-1, situation-2
etc where the numbers 1 and 2 come from the situation_number variable.

How do I create situation-1 etc such that the situation_)number
variable is used?

Thanks

Geoff
Oct 12 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Geoff Cox wrote:
Hello,

No doubt this is simple but I cannot see how to do it ...

I have a variable called situation_number with a series of values 1,
2, 3 etc.

I would like to have a series of variables situation-1, situation-2
etc where the numbers 1 and 2 come from the situation_number variable.

How do I create situation-1 etc such that the situation_)number
variable is used?
Probably the best way to loop through them is to create the variables a
members of an Array, then loop through the array. It allows you to add
more elements and use them very easily - see showVars0().

Another method is to create all the variables as globals and access them
using square bracket notation and the window object - see showVars1():

for (var i=0; i<n; ++i){
alert( window['situation-' + i] );
}

But that is not good programming practice - the global space should not
be crowded with junk that need not be there - see showVars1().

Yet another way is to use eval - see showVars(2).
<script type="text/javascript">

// Using an array
function showVars0()
{
var sit = ['sit one','sit two','sit three'];

for (var i=0, j=sit.length; i<j; ++i){
alert( 'Array: ' +sit[i] );
}
}
showVars0();

// Using global variables
function showVars1()
{
sit1 = 'sit one';
sit2 = 'sit two';
sit3 = 'sit three';

for (var i=1; i<4; ++i){
alert( 'Global: ' + window['sit'+i] );
}
}
showVars1();

// Using eval
function showVars2()
{
var sit1 = 'sit one';
var sit2 = 'sit two';
var sit3 = 'sit three';

for (var i=1; i<4; ++i){
alert( 'Eval: ' +eval('sit'+i) );
}
}
showVars2();

</script>


Thanks

Geoff

--
Rob
Oct 12 '05 #2

P: n/a
On Wed, 12 Oct 2005 07:14:59 GMT, RobG <rg***@iinet.net.au> wrote:
Probably the best way to loop through them is to create the variables a
members of an Array, then loop through the array. It allows you to add
more elements and use them very easily - see showVars0().
Rob,

Many thanks - I will read on !

Cheers

Geoff



Another method is to create all the variables as globals and access them
using square bracket notation and the window object - see showVars1():

for (var i=0; i<n; ++i){
alert( window['situation-' + i] );
}

But that is not good programming practice - the global space should not
be crowded with junk that need not be there - see showVars1().

Yet another way is to use eval - see showVars(2).
<script type="text/javascript">

// Using an array
function showVars0()
{
var sit = ['sit one','sit two','sit three'];

for (var i=0, j=sit.length; i<j; ++i){
alert( 'Array: ' +sit[i] );
}
}
showVars0();

// Using global variables
function showVars1()
{
sit1 = 'sit one';
sit2 = 'sit two';
sit3 = 'sit three';

for (var i=1; i<4; ++i){
alert( 'Global: ' + window['sit'+i] );
}
}
showVars1();

// Using eval
function showVars2()
{
var sit1 = 'sit one';
var sit2 = 'sit two';
var sit3 = 'sit three';

for (var i=1; i<4; ++i){
alert( 'Eval: ' +eval('sit'+i) );
}
}
showVars2();

</script>


Thanks

Geoff


Oct 12 '05 #3

P: n/a

"Geoff Cox" <ge*******@notquitecorrectfreeuk.com> wrote in message
news:mb********************************@4ax.com...
Hello,

No doubt this is simple but I cannot see how to do it ...

I have a variable called situation_number with a series of values 1,
2, 3 etc.

I would like to have a series of variables situation-1, situation-2
etc where the numbers 1 and 2 come from the situation_number variable.

How do I create situation-1 etc such that the situation_)number
variable is used?


this is the same answer as a simular question you posted about
getelementbyid !
Oct 12 '05 #4

P: n/a
On Wed, 12 Oct 2005 19:48:08 GMT, "Zoe Brown"
<zo***********@N-O-S-P-A-A-Mtesco.net> wrote:
this is the same answer as a simular question you posted about
getelementbyid !


You may well be right Zoe - I'm going round quite a few circles these
days!

Have got this one sorted now!

Cheers

Geoff

Oct 12 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.