Use a var to set an another var

Hi all.
I've got a question that I think the answer is simple, but the question is kinda complicated:

Let's say I have:
n = 4
And then I want to set the variable POS4 to a value. But the number after 'POS' would be the n variable value.
I've tryed all these methods below:

"POS" + n = "anything"

eval("POS" + n) = "anything"

var("POS" + n) = "anything"

var(eval("POS" + n)) = "anything"

None of them made it. Most of them say "cannot assign to a function result"
What should I do?

Thank you very much for your time.

-- Secret_Doom --

secret_doom@hotmail.com
www.batch.hpg.com.br

Comments

  • that's a tricky one you secret one...

    can't you use an array or don't you now how much elements you need... Because thats the one thing I hate most about array's that you have to know the needed size before use...

    -Rogier-

  • Hi there mac_doggie !

    I had solved my problem, but by a very precarious way:

    if (crd==1) cr1 = "value"
    if (crd==2) cr2 = "value"
    if (crd==2) cr3 = "value"

    But with your trick, it's much more simple:

    cr[crd] = "value"

    Thank you very much for the trick!

    -- Secret_Doom --

    secret_doom@hotmail.com
    www.batch.hpg.com.br

    PS: I've seen your reply after answering your last email


    ---------- mac_doggie wrote: ----------
    : that's a tricky one you secret one...
    :
    : can't you use an array or don't you now how much elements you need... Because thats the one thing I hate most about array's that you have to know the needed size before use...
    :
    : -Rogier-

  • : Hi all.
    : I've got a question that I think the answer is simple, but the question is kinda complicated:
    :
    : Let's say I have:
    : n = 4
    : And then I want to set the variable POS4 to a value. But the number after 'POS' would be the n variable value.
    : I've tryed all these methods below:
    :
    : "POS" + n = "anything"
    :
    : eval("POS" + n) = "anything"
    :
    : var("POS" + n) = "anything"
    :
    : var(eval("POS" + n)) = "anything"
    :
    : None of them made it. Most of them say "cannot assign to a function result"
    : What should I do?
    :
    : Thank you very much for your time.
    :
    : -- Secret_Doom --
    :
    : secret_doom@hotmail.com
    : www.batch.hpg.com.br
    :

    The more programmatically correct way would be to use an array, but hell this is scripting and programming rules don't matter.

    Here's a way to do exactly what you want...

    var n=4;
    eval("POS"+n+"=20;");
    alert(POS4);

    eval() executes the scrap of script passed to it as if it were code. I don't think eval() returns anything, so you can't set nothing equal to something.

    Obviously the snippet above was a specific example, but it's easy enough to generalize it.


    "There's no such thing as Hell, but you can make it if you try".
    -Prove It, Bad Religion

  • : The more programmatically correct way would be to use an array, but hell this is scripting and programming rules don't matter.
    :
    : Here's a way to do exactly what you want...
    :
    : var n=4;
    : eval("POS"+n+"=20;");
    : alert(POS4);
    :
    : eval() executes the scrap of script passed to it as if it were code. I don't think eval() returns anything, so you can't set nothing equal to something.
    :
    : Obviously the snippet above was a specific example, but it's easy enough to generalize it.
    :
    :
    : "There's no such thing as Hell, but you can make it if you try".
    : -Prove It, Bad Religion


    Hi Darius.
    That is EXATELY what I wanted. Both of the solutions (yours and mac_doggie's one) worked just fine.

    Oh, btw, I think eval() DOES return something. Watch this:

    n = 4
    alert( document.eval("form" + n).textfield.value )
    //The two lines above make the same thing as the following
    alert( document.form4.textfield.value )

    Got it?

    Thank you very much for your help!

    -- Secret_Doom - Leonardo Pignataro --

    secret_doom@hotmail.com
    www.batch.hpg.com.br
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories