Creating a variable name as the value of another variable.

Discussion in 'ASP General' started by rdstevenson@hotmail.co.uk, Oct 11, 2007.

  1. Guest

    Hi,

    Something I've never come across before and has given me a headache.
    I have a txt file with content similar to below I want to read in each
    line do a split on the = and then create a variable with the name of
    the first value and the value of the second. I'm fine with the
    splitting etc but I haven't got a clue of how to set the variable name
    to the value on another variable?

    any ideas anyone?



    Variable1=Value1
    Variable2=Value2
    etc etc
     
    , Oct 11, 2007
    #1
    1. Advertising

  2. wrote:
    > Hi,
    >
    > Something I've never come across before and has given me a headache.
    > I have a txt file with content similar to below I want to read in each
    > line do a split on the = and then create a variable with the name of
    > the first value and the value of the second. I'm fine with the
    > splitting etc but I haven't got a clue of how to set the variable name
    > to the value on another variable?
    >
    > any ideas anyone?
    >
    >
    >
    > Variable1=Value1
    > Variable2=Value2
    > etc etc


    Why do you need dynamic variable names? If you could explain the necessity,
    perhaps we could suggest an alternative, such as a Dictionary object.

    --
    Microsoft MVP - ASP/ASP.NET
    Please reply to the newsgroup. This email account is my spam trap so I
    don't check it very often. If you must reply off-line, then remove the
    "NO SPAM"
     
    Bob Barrows [MVP], Oct 11, 2007
    #2
    1. Advertising

  3. jp2code Guest

    Your variables have to be declared before you can execute your code. How
    would you be able to create a variable named, for example, Variable1 until
    the program is executed and you actually encounter it.

    You could create an array of 2 element arrays! The first element could
    contain the name of Variable1, and the second element could contain the
    value.

    If this doesn't help, you might want to expand on your question.

    Regards,
    "Bondo" Joe

    <> wrote:
    > Hi,
    >
    > Something I've never come across before and has given me a headache.
    > I have a txt file with content similar to below I want to read in each
    > line do a split on the = and then create a variable with the name of
    > the first value and the value of the second. I'm fine with the
    > splitting etc but I haven't got a clue of how to set the variable name
    > to the value on another variable?
    >
    > any ideas anyone?
    >
    >
    >
    > Variable1=Value1
    > Variable2=Value2
    > etc etc
    >
     
    jp2code, Oct 11, 2007
    #3
  4. "jp2code" <poojo.com/mail> wrote in message
    news:...
    > Your variables have to be declared before you can execute your code. How
    > would you be able to create a variable named, for example, Variable1 until
    > the program is executed and you actually encounter it.


    Actually, the VBS Execute statement allows you to execute script code,
    passed to it in a variable. It is possible to dynamically construct
    variable declarations, assignments, functions, etc, and then run the
    generated code in the same context as the static part of the script.

    That part is easy; coming up with a valid and compelling reason to use this
    is the challenge. In the case of dynamically declared variables, what good
    are they, unless they're referenced elsewhere in the code (and if that was
    the case, they really wouldn't be all that dynamic, would they?

    As for the OP, if the .txt file contains legal VBS assignments, why split,
    why not just execute the whole file? Otoh, allowing unknown script to be
    executed on your server is pretty low on the list of safe and sane things
    for your site to facilitate.

    In almost all cases there are better design options to be had by using
    arrays or a dictionary... in fact, the overwhelming majority of reasons
    people think they want to write self-modifying code are really, really lame.
    So to the OP, consider all options first; use Execute as a last resort.


    -Mark


    > You could create an array of 2 element arrays! The first element could
    > contain the name of Variable1, and the second element could contain the
    > value.
    >
    > If this doesn't help, you might want to expand on your question.
    >
    > Regards,
    > "Bondo" Joe
    >
    > <> wrote:
    >> Hi,
    >>
    >> Something I've never come across before and has given me a headache.
    >> I have a txt file with content similar to below I want to read in each
    >> line do a split on the = and then create a variable with the name of
    >> the first value and the value of the second. I'm fine with the
    >> splitting etc but I haven't got a clue of how to set the variable name
    >> to the value on another variable?
    >>
    >> any ideas anyone?
    >>
    >>
    >>
    >> Variable1=Value1
    >> Variable2=Value2
    >> etc etc
    >>

    >
    >
     
    Mark J. McGinty, Oct 12, 2007
    #4
  5. Mark J. McGinty wrote:

    > In almost all cases there are better design options to be had by using
    > arrays or a dictionary... in fact, the overwhelming majority of
    > reasons people think they want to write self-modifying code are
    > really, really lame. So to the OP, consider all options first; use
    > Execute as a last resort.

    ..
    .... and [also to the OP] the reason why Execute should be avoided? Because
    every use of Execute in your code causes a new instance of the vbscript
    compiler to be spawned, impairing performance and utilizing extra server
    resources.

    --
    Microsoft MVP - ASP/ASP.NET
    Please reply to the newsgroup. This email account is my spam trap so I
    don't check it very often. If you must reply off-line, then remove the
    "NO SPAM"
     
    Bob Barrows [MVP], Oct 12, 2007
    #5
  6. "Bob Barrows [MVP]" <> wrote in message
    news:...
    > Mark J. McGinty wrote:
    >
    >> In almost all cases there are better design options to be had by using
    >> arrays or a dictionary... in fact, the overwhelming majority of
    >> reasons people think they want to write self-modifying code are
    >> really, really lame. So to the OP, consider all options first; use
    >> Execute as a last resort.

    > .
    > ... and [also to the OP] the reason why Execute should be avoided? Because
    > every use of Execute in your code causes a new instance of the vbscript
    > compiler to be spawned, impairing performance and utilizing extra server
    > resources.


    ....spawns a new instance of the vbscript compiler? Ooook... sounds
    serious... I wonder if it spawns the vbscript linker too?

    I don't suppose you have a link about this? I'd like to read more, but
    can't seem to find anything on google.


    -Mark


    > --
    > Microsoft MVP - ASP/ASP.NET
    > Please reply to the newsgroup. This email account is my spam trap so I
    > don't check it very often. If you must reply off-line, then remove the
    > "NO SPAM"
    >
     
    Mark J. McGinty, Oct 13, 2007
    #6
  7. Mark J. McGinty wrote:
    > "Bob Barrows [MVP]" <> wrote in message
    > news:...
    >> Mark J. McGinty wrote:
    >>
    >>> In almost all cases there are better design options to be had by
    >>> using arrays or a dictionary... in fact, the overwhelming majority
    >>> of reasons people think they want to write self-modifying code are
    >>> really, really lame. So to the OP, consider all options first; use
    >>> Execute as a last resort.

    >> .
    >> ... and [also to the OP] the reason why Execute should be avoided?
    >> Because every use of Execute in your code causes a new instance of
    >> the vbscript compiler to be spawned, impairing performance and
    >> utilizing extra server resources.

    >
    > ...spawns a new instance of the vbscript compiler? Ooook... sounds
    > serious... I wonder if it spawns the vbscript linker too?
    >
    > I don't suppose you have a link about this? I'd like to read more,
    > but can't seem to find anything on google.
    >
    >

    Google for Eric Lippert's "famous" "Eval is Evil" blog entry (Fabulous
    Adventures in Coding)

    --
    Microsoft MVP - ASP/ASP.NET
    Please reply to the newsgroup. This email account is my spam trap so I
    don't check it very often. If you must reply off-line, then remove the
    "NO SPAM"
     
    Bob Barrows [MVP], Oct 13, 2007
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Brian
    Replies:
    3
    Views:
    442
    Siemel Naran
    Apr 19, 2004
  2. Nmishra
    Replies:
    3
    Views:
    377
    Default User
    May 24, 2004
  3. Bobby Chamness
    Replies:
    2
    Views:
    2,426
    Joe Smith
    Apr 22, 2007
  4. loial
    Replies:
    7
    Views:
    399
    Diez B. Roggisch
    May 1, 2007
  5. Robin Corcoran

    Use a variable value in another variable's name

    Robin Corcoran, Jul 22, 2004, in forum: Perl Misc
    Replies:
    20
    Views:
    319
    Brian McCauley
    Jul 26, 2004
Loading...

Share This Page