variables setting w/array

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • danny

    #1

    variables setting w/array

    Hello everyone,

    How can I go about setting a number of variables using a table which will
    have the variable names to be declared in one table. The data to be assigned
    will reside in another. As an example, I know this assignment works:

    var nam0 = tab[0]
    var nam1 = tab[1]
    var nam2 = tab[2]
    ..
    ..
    ..

    What I would like is to use two tables

    var namtab = ["nam0","nam1"," nam2"]
    var tab = ["va0","val1","v al2"]

    Then using a loop, assign the variables so that I may address them by name
    "nam0" rather than namtab[0].

    var nametab = ["nam0","nam1"," nam2"] ** also tried w/o the quotes
    for(var i=0;i<3;i++) {
    var nametab[i] = crumbs[0];

    Thanks,

    From a newbie (but learning) .


  • Janwillem Borleffs

    #2
    Re: variables setting w/array

    danny wrote:[color=blue]
    > What I would like is to use two tables
    >
    > var namtab = ["nam0","nam1"," nam2"]
    > var tab = ["va0","val1","v al2"]
    >
    > Then using a loop, assign the variables so that I may address them by
    > name "nam0" rather than namtab[0].
    >
    > var nametab = ["nam0","nam1"," nam2"] ** also tried w/o the quotes
    > for(var i=0;i<3;i++) {
    > var nametab[i] = crumbs[0];
    >[/color]

    Something like this?

    var nametab = ["nam0","nam1"," nam2"];
    var tab = ["va0","val1","v al2"];

    for (var i = 0; i < nametab.length; i++) {
    window[nametab[i]] = tab[i];
    }

    // Testing
    alert(nam0) // Alerts 'va0'


    JW



    Comment

    • Lee

      #3
      Re: variables setting w/array

      danny said:[color=blue]
      >
      >Hello everyone,
      >
      >How can I go about setting a number of variables using a table which will
      >have the variable names to be declared in one table. The data to be assigned
      >will reside in another. As an example, I know this assignment works:
      >
      >var nam0 = tab[0]
      >var nam1 = tab[1]
      >var nam2 = tab[2]
      >.
      >.
      >.
      >
      >What I would like is to use two tables
      >
      >var namtab = ["nam0","nam1"," nam2"]
      >var tab = ["va0","val1","v al2"]
      >
      >Then using a loop, assign the variables so that I may address them by name
      >"nam0" rather than namtab[0].[/color]

      I can't imagine a situation where you would want to do that.

      If you wanted to be able to refer to them as nam[0] rather than
      namtab[0], you can do that simply by:

      var nam=namtab;

      Note that this doesn't make a complete new copy of the array,
      it just makes "nam" refer to the same data.

      Comment

      • danny

        #4
        Re: variables setting w/array

        JW,

        Thanks. It worked as expected. Could you just tell me why I needed to use
        the statement 'window[nametab[i]] = tab[i];'
        instead of just using 'nametab[i] = tab[i];'. Since both tables are in
        the same window/document, why must the 'window' method(?) be supplied?

        danny

        "danny" <dthomas@plan et-inc.net> wrote in message
        news:112mcpmc89 rg1b7@corp.supe rnews.com...[color=blue]
        > Hello everyone,
        >
        > How can I go about setting a number of variables using a table which will
        > have the variable names to be declared in one table. The data to be[/color]
        assigned[color=blue]
        > will reside in another. As an example, I know this assignment works:
        >
        > var nam0 = tab[0]
        > var nam1 = tab[1]
        > var nam2 = tab[2]
        > .
        > .
        > .
        >
        > What I would like is to use two tables
        >
        > var namtab = ["nam0","nam1"," nam2"]
        > var tab = ["va0","val1","v al2"]
        >
        > Then using a loop, assign the variables so that I may address them by name
        > "nam0" rather than namtab[0].
        >
        > var nametab = ["nam0","nam1"," nam2"] ** also tried w/o the quotes
        > for(var i=0;i<3;i++) {
        > var nametab[i] = crumbs[0];
        >
        > Thanks,
        >
        > From a newbie (but learning) .
        >
        >[/color]


        Comment

        • RobB

          #5
          Re: variables setting w/array

          > > "danny" <dthomas@plan et-inc.net> wrote in message[color=blue]
          > news:112mcpmc89 rg1b7@corp.supe rnews.com...[color=green]
          > > Hello everyone,
          > >
          > > How can I go about setting a number of variables using a table[/color][/color]
          which will[color=blue][color=green]
          > > have the variable names to be declared in one table. The data to be[/color]
          > assigned[color=green]
          > > will reside in another. As an example, I know this assignment[/color][/color]
          works:[color=blue][color=green]
          > >
          > > var nam0 = tab[0]
          > > var nam1 = tab[1]
          > > var nam2 = tab[2]
          > > .
          > > .
          > > .
          > >
          > > What I would like is to use two tables
          > >
          > > var namtab = ["nam0","nam1"," nam2"]
          > > var tab = ["va0","val1","v al2"]
          > >
          > > Then using a loop, assign the variables so that I may address them[/color][/color]
          by name[color=blue][color=green]
          > > "nam0" rather than namtab[0].
          > >
          > > var nametab = ["nam0","nam1"," nam2"] ** also tried w/o the[/color][/color]
          quotes[color=blue][color=green]
          > > for(var i=0;i<3;i++) {
          > > var nametab[i] = crumbs[0];
          > >
          > > Thanks,
          > >
          > > From a newbie (but learning) .
          > >[/color][/color]
          [color=blue]
          >danny wrote:
          > JW,
          >
          > Thanks. It worked as expected. Could you just tell me why I needed to[/color]
          use[color=blue]
          > the statement 'window[nametab[i]] = tab[i];'
          > instead of just using 'nametab[i] = tab[i];'. Since both tables[/color]
          are in[color=blue]
          > the same window/document, why must the 'window' method(?) be[/color]
          supplied?[color=blue]
          >
          > danny[/color]


          Watch the top-posting !

          Not a 'window method' - just a way to express an identifier (variable
          name) as a string, explained here:



          Instead of cluttering the global namespace with all those variables,
          why not populate a global array instead?

          <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
          "http://www.w3.org/TR/html4/strict.dtd">
          <html>
          <head>
          <title>untitled </title>
          <script type="text/javascript">

          var nametab = ["nam0","nam1"," nam2"];
          var tab = ["val0","val1"," val2"];

          for (var tabs = [], i = 0, ntl = nametab.length; i < ntl; ++i)
          {
          tabs[nametab[i]] = tab[i];
          }

          </script>
          </head>
          <body>
          <form>
          <select onchange="alert (tabs[this.value]||'not in')">
          <option value="">select </option>
          <option value="nam0">na m0</option>
          <option value="nam1">na m1</option>
          <option value="nam2">na m2</option>
          </select>
          </form>
          </body>
          </html>

          Comment

          • RobB

            #6
            Re: variables setting w/array

            RobB wrote:[color=blue][color=green][color=darkred]
            > > > "danny" <dthomas@plan et-inc.net> wrote in message[/color]
            > > news:112mcpmc89 rg1b7@corp.supe rnews.com...[color=darkred]
            > > > Hello everyone,
            > > >
            > > > How can I go about setting a number of variables using a table[/color][/color]
            > which will[color=green][color=darkred]
            > > > have the variable names to be declared in one table. The data to[/color][/color][/color]
            be[color=blue][color=green]
            > > assigned[color=darkred]
            > > > will reside in another. As an example, I know this assignment[/color][/color]
            > works:[color=green][color=darkred]
            > > >
            > > > var nam0 = tab[0]
            > > > var nam1 = tab[1]
            > > > var nam2 = tab[2]
            > > > .
            > > > .
            > > > .
            > > >
            > > > What I would like is to use two tables
            > > >
            > > > var namtab = ["nam0","nam1"," nam2"]
            > > > var tab = ["va0","val1","v al2"]
            > > >
            > > > Then using a loop, assign the variables so that I may address[/color][/color][/color]
            them[color=blue]
            > by name[color=green][color=darkred]
            > > > "nam0" rather than namtab[0].
            > > >
            > > > var nametab = ["nam0","nam1"," nam2"] ** also tried w/o the[/color][/color]
            > quotes[color=green][color=darkred]
            > > > for(var i=0;i<3;i++) {
            > > > var nametab[i] = crumbs[0];
            > > >
            > > > Thanks,
            > > >
            > > > From a newbie (but learning) .
            > > >[/color][/color]
            >[color=green]
            > >danny wrote:
            > > JW,
            > >
            > > Thanks. It worked as expected. Could you just tell me why I needed[/color][/color]
            to[color=blue]
            > use[color=green]
            > > the statement 'window[nametab[i]] = tab[i];'
            > > instead of just using 'nametab[i] = tab[i];'. Since both[/color][/color]
            tables[color=blue]
            > are in[color=green]
            > > the same window/document, why must the 'window' method(?) be[/color]
            > supplied?[color=green]
            > >
            > > danny[/color]
            >
            >
            > Watch the top-posting !
            >
            > Not a 'window method' - just a way to express an identifier (variable
            > name) as a string, explained here:
            >
            > http://jibbering.com/faq/faq_notes/s...ckets.html#aVa
            >
            > Instead of cluttering the global namespace with all those variables,
            > why not populate a global array instead?
            >
            > <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
            > "http://www.w3.org/TR/html4/strict.dtd">
            > <html>
            > <head>
            > <title>untitled </title>
            > <script type="text/javascript">
            >
            > var nametab = ["nam0","nam1"," nam2"];
            > var tab = ["val0","val1"," val2"];
            >
            > for (var tabs = [], i = 0, ntl = nametab.length; i < ntl; ++i)
            > {
            > tabs[nametab[i]] = tab[i];
            > }
            >
            > </script>
            > </head>
            > <body>
            > <form>
            > <select onchange="alert (tabs[this.value]||'not in')">
            > <option value="">select </option>
            > <option value="nam0">na m0</option>
            > <option value="nam1">na m1</option>
            > <option value="nam2">na m2</option>
            > </select>
            > </form>
            > </body>
            > </html>[/color]

            OK, before one of the regulars yells at me: if you're not using that
            array for any other purpose - use a plain-vanilla object instead:

            for (var tabs = {},....

            Comment

            • c.anandkumar@gmail.com

              #7
              Re: variables setting w/array

              If you really want to use those variables with distinct names of the
              form namN, rather than bounding all of them in an array, then use
              eval() function. Im pasting a similar function below :

              var tabs = new Array ("tZero", "tOne", "tTwo", "tThree");
              function variables ()
              {
              var i;
              for (i = 0; i < tabs.length; i++) {
              eval ('nam' + i + '=tabs[' + i + ']');
              }
              alert (nam0);
              alert (nam1);
              // etc...
              }

              you pass a valid js statement to eval to cause the statement to be
              executed runtime.

              Thanks

              Anand.

              Comment

              • Grant Wagner

                #8
                Re: variables setting w/array

                <c.anandkumar@g mail.com> wrote in message
                news:1110186448 .104014.312670@ g14g2000cwa.goo glegroups.com.. .[color=blue]
                > If you really want to use those variables with distinct names of the
                > form namN, rather than bounding all of them in an array, then use
                > eval() function. Im pasting a similar function below :
                >
                > var tabs = new Array ("tZero", "tOne", "tTwo", "tThree");
                > function variables ()
                > {
                > var i;
                > for (i = 0; i < tabs.length; i++) {
                > eval ('nam' + i + '=tabs[' + i + ']');
                > }
                > alert (nam0);
                > alert (nam1);
                > // etc...
                > }
                >
                > you pass a valid js statement to eval to cause the statement to be
                > executed runtime.
                >
                > Thanks
                >
                > Anand.[/color]

                eval() is not required. Assuming you want those variable names to be
                global, you could use:

                var tabs = new Array ("tZero", "tOne", "tTwo", "tThree");
                function variables ()
                {
                var i;
                for (i = 0; i < tabs.length; i++)
                {
                window['nam' + i] = tabs[i];
                }
                alert (nam0);
                alert (nam1);
                // etc...
                }

                --
                Grant Wagner <gwagner@agrico reunited.com>
                comp.lang.javas cript FAQ - http://jibbering.com/faq


                Comment

                Working...