Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    New Coder
    Join Date
    Dec 2008
    Posts
    58
    Thanks
    18
    Thanked 1 Time in 1 Post

    Desperate! Putting a variable in a variable name?

    Okay the following block of code is for demonstrative purposes only:

    Code:
    for (x=0;x<=2;x++) { 
    	game + x = "Game "+x;
    }
    Okay so looking at that you can probably get the feeling for what I want to do. Please do not ask why I want to do this, as obviously the above code makes no sense and can be done with arrays, but I assure you I have a purpose for doing what I want to do.

    The question is, can I put a variable, like x, into a new variable's name? The idea is that the code above generates three new variables:
    game0
    game1
    game2

    I've tried using brackets and parentheses, I've tries so many things. I've even tried using the eval() function.

    So, again, in short, can I place a variable into another variable's name?

    A hasty response is much appreciated.

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,144
    Thanks
    203
    Thanked 2,547 Times in 2,525 Posts
    Quote Originally Posted by TheTot View Post
    So, again, in short, can I place a variable into another variable's name?
    A hasty response is much appreciated.
    My hasty response is - no.

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,536
    Thanks
    80
    Thanked 4,490 Times in 4,454 Posts
    LOL!

    A less hasty response would be: DO NOT DO THIS!

    Use an array:

    Code:
    var game = new Array();
    for ( i = 0; i < 3; ++i )
    {
        game[i] = "Game number " + i;
    }
    And now, where you HAD been planning to use game1 (etc.), just use game[1] instead.

    And even longer answer is: Yes, you can do it with eval( ). But just remember that eval( ) is only a misspelling of evil( ) and you want to avoid evil when you can.

  • #4
    New Coder
    Join Date
    Dec 2008
    Posts
    58
    Thanks
    18
    Thanked 1 Time in 1 Post
    Quote Originally Posted by Old Pedant View Post
    LOL!

    A less hasty response would be: DO NOT DO THIS!

    Use an array:

    Code:
    var game = new Array();
    for ( i = 0; i < 3; ++i )
    {
        game[i] = "Game number " + i;
    }
    And now, where you HAD been planning to use game1 (etc.), just use game[1] instead.

    And even longer answer is: Yes, you can do it with eval( ). But just remember that eval( ) is only a misspelling of evil( ) and you want to avoid evil when you can.
    I said the code I provided was the simplest of examples. I know I could have used an array, but I'm doing something more.

    I'm pretty much trying to simulate what PHP has called "multidimensional" arrays. Because we're actually going to be using it to generate complex variable names, such as
    game#_character#

    And yes, I ended up using the eval() function, because it works.

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,144
    Thanks
    203
    Thanked 2,547 Times in 2,525 Posts
    Quote Originally Posted by TheTot View Post
    And yes, I ended up using the eval() function, because it works.

    The guy said he did not want to use arrays.

    No-one has ever explained to me convincingly why eval() is evil. I do understand that it is (relatively) slow, and may pose a security risk if permitted in an input field. I accept that alternatives are to be preferred where possible. Otherwise - what is the problem?

  • Users who have thanked Philip M for this post:

    TheTot (05-22-2009)

  • #6
    New Coder
    Join Date
    Dec 2008
    Posts
    58
    Thanks
    18
    Thanked 1 Time in 1 Post
    Quote Originally Posted by Philip M View Post
    The guy said he did not want to use arrays.

    No-one has ever explained to me convincingly why eval() is evil. I do understand that it is (relatively) slow, and may pose a security risk if permitted in an input field. I accept that alternatives are to be preferred where possible. Otherwise - what is the problem?
    Thanks for actually paying attention to my post. -_-; I've been coding for a little while now, I know what an array is. =)

    This wouldn't be a problem if it was php cause they have multidimensional arrays which are like, arrays inside of an array, it's crazy convenient. But javascript doesn't have that, and what me and my co-webmaster need to do is create just a few variables that could potentially be named like a billion things. So it'd be a real inconvenience to go through and manually add those billion possibilities.

    But yeah, I never got it either. This isn't the first time I've been told not to use eval(), but I actually do use it on several occasions in quite a few of my scripts, mostly to do things like I'm asking here. I use it to call function names that are dependent upon a variable, and this time we were trying to make a variable name that is dependent on another variable.

    Btw, I ended up achieving the desired result, but I had to wrap eval around the entire line, for example..
    Code:
    eval("game"+x"+_char"+y+" = etcetcetc");

  • #7
    GŁtkodierer
    Join Date
    Apr 2009
    Posts
    2,127
    Thanks
    1
    Thanked 426 Times in 424 Posts
    Philip M:

    Apart from the fact that evals can become stupidly difficult to read, debug and maintain, it's probably always a warning sign, that the coder does not know what he is doing, or better, that he does not know what he could or should be doing, had he a better knowledge of Javascript and best practices.

    There is a bit of a misconception here:
    Good programmers won't ever get themselves in a situation where they are forced to give up their good coding style and use something like eval instead, and as a consequence it is often believed that telling people with little experience and an overall poor programming style not to use evals will make them better programmers.

    That's certainly not true, but it may be a reason for all that eval-bashing out there.

    I believe people should use what they manage to get to work with their level of experience, but when using things that are generally considered bad style, they should ask themselves (and others!), what they could do differently.


    TheTot:

    Let me use you as an example, and try to help you at the same time:

    First of all, you are writing above The idea is that the code above generates three new variables. What you are doing there is generating global variables, which is generally bad style, but if you really want to (or don't know what else to do), you can easily access global variables via the window object.
    eval("game"+x"+_char"+y+" = etcetcetc"); can easily be rewritten to window["game" + x + "_char" + y] = etcetcetc;.

    Secondly: JS does not have multidimensional arrays? How did you get to believe that? That's just plain wrong. There's absolutely no problem with using a two dimensional array game_character and access it like game_character[x][y].

    Thirdly: If for some reason you really love the idea of actual variable variable names, just put them in a custom object, will you? You don't want all those billion possibilities flying around freely and crapping all over your global namespace. Something like var myVars = {}; myVars["game" + x + "_char" + y] = ... will suffice. You can access them like above or i.e. as myVars.game0_char1.
    Last edited by venegal; 05-22-2009 at 03:56 PM.

  • #8
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,144
    Thanks
    203
    Thanked 2,547 Times in 2,525 Posts
    Methinks you do protest too much. And I did say "I accept that alternatives are to be preferred where possible."

    http://stackoverflow.com/questions/8...ion-a-bad-idea

    seems to be a balanced view. See also:-

    http://articles.techrepublic.com.com...1-5169823.html

    http://stackoverflow.com/questions/1...ot-evil/197790
    Last edited by Philip M; 05-22-2009 at 05:15 PM.

  • #9
    GŁtkodierer
    Join Date
    Apr 2009
    Posts
    2,127
    Thanks
    1
    Thanked 426 Times in 424 Posts
    I believe my protest against TheTot's use of eval is very appropriate. Is there a specific point you disagree with?

    Anyway, I did read what you wrote, and I think, my opinion does not differ that much from yours. In my opinion eval is a very specific command with a very specific intended usage, which is unfortunately very very often exploited to do things it isn't there for, just because it's very easy to do. In that way, it is very similar to HTML's <table>.

    That said, the OP's usage of eval is just plain wrong, and I think it's harmful to tell an inexperienced coder it would be ok not to use arrays if he does not want to, when it clearly is not and he just doesn't know how to do it.

    The OP thanking you for that post shows that next time he gets himself into a similar problem he will just use the wrong tools all over again, which can't be a good thing.

  • #10
    Regular Coder
    Join Date
    Apr 2009
    Posts
    244
    Thanks
    1
    Thanked 20 Times in 20 Posts

    Desperate! Putting a variable in a variable name?

    Hey,

    If all you are doing is specifically and explicitly simulating the use of multidimensional arrays, then I say just use multidimensional arrays. It is by far not a "feature" branded by PHP. In fact, I don't know a language where you couldn't implement multidimensional arrays. This of course includes JavaScript(and I can tell you that in JS I've personally used them in the past). Here is an example:
    http://www.devx.com/tips/Tip/12455

    If you say that you absolutely need to use Eval, then that's all I need in order for you to convince me. I believe that there are situations out there where it's imperative that you use it. Such as if somebody's holding a gun to your head, and demanding you to use it. No, im just joking. But really, when I work in MSSQL Server for example, I sometimes build a dynamic SQL and then execute it using EXEC() function, so it's something similar I guess.

    I will not make some involuntary robotic response claiming that using Eval is "evil", or anything like that. But all I can say is...be very careful, and consider ***ALL*** other alternatives beforehand. Reasons for me saying so are clearly stated in the very links that you provided.

    Regards,
    Mike

  • #11
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,144
    Thanks
    203
    Thanked 2,547 Times in 2,525 Posts
    Quote Originally Posted by Mike_O View Post
    Hey,
    I will not make some involuntary robotic response claiming that using Eval is "evil", or anything like that. But all I can say is...be very careful, and consider ***ALL*** other alternatives beforehand. Reasons for me saying so are clearly stated in the very links that you provided.
    I think we can all agree with that.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •