address array by variable variable

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • mps@webmind.nl

    address array by variable variable

    Suppose.....


    arr = Array("only","a ","test");
    ArrayVar = "arr";
    alert(ArrayVar[0]);

    This the simplyfied version of my problem.. (alerting 'undefined' instead of
    'only'...)

    Now I use the most ugly workaround:
    if(ArrayVar == "arr") { alert(arr[0]); }

    But there has to be a much better solution?????

    Thanx in advance,
    Marco Snoek


  • Vincent van Beveren

    #2
    Re: address array by variable variable

    > Suppose.....[color=blue]
    >
    >
    > arr = Array("only","a ","test");
    > ArrayVar = "arr";
    > alert(ArrayVar[0]);
    >
    > This the simplyfied version of my problem.. (alerting 'undefined' instead of
    > 'only'...)[/color]

    I think this is what you are looking for:

    eval(ArrayVar+"[0]");

    But you can also do the following

    arr1 = Array("only", "a", "test");
    arr2 = Array("only", "another", "test");

    curArray = arr1;
    alert(curArray[1]); // displays 'a';

    curArray = arr2;
    alert(curArray[1]); // displays 'another';

    Which feels better.

    Good luck,
    Vincent


    Comment

    • Ivo

      #3
      Re: address array by variable variable

      "mps@webmind.nl " wrote[color=blue]
      >
      > arr = Array("only","a ","test");
      > ArrayVar = "arr";
      > alert(ArrayVar[0]);
      >
      > This the simplyfied version of my problem.. (alerting 'undefined' instead[/color]
      of[color=blue]
      > 'only'...)
      >
      > Now I use the most ugly workaround:
      > if(ArrayVar == "arr") { alert(arr[0]); }
      >
      > But there has to be a much better solution?????[/color]

      Not sure what you are trying to do. ArrayVar is a now string variable. If
      you remove the quotes from your second line above, so it becomes

      ArrayVar = arr;

      then ArrayVar will be an array too, and ArrayVar[0] will be "only".
      HTH
      Ivo


      Comment

      • lallous

        #4
        Re: address array by variable variable

        Hello

        "mps@webmind.nl "
        <*DONT*[mps]_YOU#DARE(SEND) SPAM@@[webmind]--TOME.[.nl]%%^.orgtext> wrote in
        message news:40fbc710$0 $78439$e4fe514c @dreader5.news. xs4all.nl...[color=blue]
        > Suppose.....
        >
        >
        > arr = Array("only","a ","test");
        > ArrayVar = "arr";
        > alert(ArrayVar[0]);
        >
        > This the simplyfied version of my problem.. (alerting 'undefined' instead[/color]
        of[color=blue]
        > 'only'...)
        >
        > Now I use the most ugly workaround:
        > if(ArrayVar == "arr") { alert(arr[0]); }
        >
        > But there has to be a much better solution?????
        >[/color]

        Yes, you can use eval() as other indicated or you can use something like:

        <script>
        var x;

        array1 = new Array("hello", "world");

        x = "array1";

        alert(window[x][0]); // <-- hello

        </script>


        The variables are stored under the window object. You can also do something
        like: window["array1"][N] ...

        --
        Elias


        Comment

        • Quarco

          #5
          Re: address array by variable variable

          That is my problem: trying to retrieve an array item with a string
          variable-name..
          You see, the more elaborate version of my problem is

          an array with positions:
          positions =
          ("liggend","sta and","staand"," liggend","staan d","staand","li ggend");

          an array
          liggend[0] = array("value1a" ,"value2a","val ue3a","value4a" ,"value5a");
          liggend[1] = array("value1b" ,"value2b","val ue3b","value4b" ,"value5b");
          ....
          staand[0] = array("value1c" ,"value2c","val ue3c","value4c" ,"value5c");
          ...etc..

          Now:
          getValue(3) { //For example...
          ArrayVar = positions[3]; // in this example ArrayVar = liggend
          for(anItem in ArrayVar) {
          ...... /// walk through the 'liggend-array'..
          }
          }

          I believe the eval function from Vincent does the trick...


          Marco



          "Ivo" <no@thank.you > schreef in bericht
          news:40fbcac4$0 $62721$ee9da40f @news.wanadoo.n l...[color=blue]
          > "mps@webmind.nl " wrote[color=green]
          > >
          > > arr = Array("only","a ","test");
          > > ArrayVar = "arr";
          > > alert(ArrayVar[0]);
          > >
          > > This the simplyfied version of my problem.. (alerting 'undefined'[/color][/color]
          instead[color=blue]
          > of[color=green]
          > > 'only'...)
          > >
          > > Now I use the most ugly workaround:
          > > if(ArrayVar == "arr") { alert(arr[0]); }
          > >
          > > But there has to be a much better solution?????[/color]
          >
          > Not sure what you are trying to do. ArrayVar is a now string variable. If
          > you remove the quotes from your second line above, so it becomes
          >
          > ArrayVar = arr;
          >
          > then ArrayVar will be an array too, and ArrayVar[0] will be "only".
          > HTH
          > Ivo
          >
          >[/color]


          Comment

          • Quarco

            #6
            Re: address array by variable variable

            Hey,

            Great..
            It works!!

            Thanx all for your time!!

            Regards,
            Marco


            "lallous" <lallous@lgwm.o rg> schreef in bericht
            news:2m22a7Fi7a iuU1@uni-berlin.de...[color=blue]
            > Hello
            >
            > "mps@webmind.nl "
            > <*DONT*[mps]_YOU#DARE(SEND) SPAM@@[webmind]--TOME.[.nl]%%^.orgtext> wrote[/color]
            in[color=blue]
            > message news:40fbc710$0 $78439$e4fe514c @dreader5.news. xs4all.nl...[color=green]
            > > Suppose.....
            > >
            > >
            > > arr = Array("only","a ","test");
            > > ArrayVar = "arr";
            > > alert(ArrayVar[0]);
            > >
            > > This the simplyfied version of my problem.. (alerting 'undefined'[/color][/color]
            instead[color=blue]
            > of[color=green]
            > > 'only'...)
            > >
            > > Now I use the most ugly workaround:
            > > if(ArrayVar == "arr") { alert(arr[0]); }
            > >
            > > But there has to be a much better solution?????
            > >[/color]
            >
            > Yes, you can use eval() as other indicated or you can use something like:
            >
            > <script>
            > var x;
            >
            > array1 = new Array("hello", "world");
            >
            > x = "array1";
            >
            > alert(window[x][0]); // <-- hello
            >
            > </script>
            >
            >
            > The variables are stored under the window object. You can also do[/color]
            something[color=blue]
            > like: window["array1"][N] ...
            >
            > --
            > Elias
            >
            >[/color]


            Comment

            • Lee

              #7
              Re: address array by variable variable

              SEND said:[color=blue]
              >
              >Suppose.....
              >
              >
              >arr = Array("only","a ","test");
              >ArrayVar = "arr";
              >alert(ArrayV ar[0]);
              >
              >This the simplyfied version of my problem.. (alerting 'undefined' instead of
              >'only'...)
              >
              >Now I use the most ugly workaround:
              >if(ArrayVar == "arr") { alert(arr[0]); }
              >
              >But there has to be a much better solution?????[/color]

              It depends on what you're really trying to do. If you just need
              a way to use a variable name for an array, then you can use a
              reference, instead of a string holding the name:

              var myRef=arr;
              alert(myRef[0]);

              If you really need to use the string name, then there is probably
              a better design that you could use, but one way to do it is:

              var arrayGroup = { arr: [ "only", "a", "test" ],
              brr: [ "a", "different" , "array" ],
              crr: [ "one", "more", "example" ]
              };
              alert(arrayGrou p["arr"][0]);


              You could also use eval(), but that is inefficient and difficult
              to debug.

              Comment

              • Lasse Reichstein Nielsen

                #8
                Re: address array by variable variable

                "Quarco" <*DONT*[mps]_YOU#DARE(SEND) SPAM@@[webmind]--TOME.[.nl]%%^.orgtext> writes:
                [color=blue]
                > That is my problem: trying to retrieve an array item with a string
                > variable-name..[/color]

                In my experience, you are using a wrong approach to solve the real problem.
                I have never seen a case where it was necessary to address a variable
                using a string containing its name. Whatever you are trying to do, I'll
                almost guarantee I can find a better way to do it :)
                [color=blue]
                > an array with positions:
                > positions =
                > ("liggend","sta and","staand"," liggend","staan d","staand","li ggend");
                >
                > an array
                > liggend[0] = array("value1a" ,"value2a","val ue3a","value4a" ,"value5a");
                > liggend[1] = array("value1b" ,"value2b","val ue3b","value4b" ,"value5b");
                > ...
                > staand[0] = array("value1c" ,"value2c","val ue3c","value4c" ,"value5c");
                > ..etc..[/color]

                Instead, do:

                var c = new Object(); // container object
                c.liggend = new Array();
                c.staand = new Array();
                and then
                c.liggend[0] = ...
                c.liggend[1] = ...
                c.liggend[2] = ...
                ...
                c.staand[0] = ...
                c.staand[1] = ...
                [color=blue]
                > getValue(3) { //For example...
                > ArrayVar = positions[3]; // in this example ArrayVar = liggend
                > for(anItem in ArrayVar) {[/color]

                function getValue(i) {
                var arrayVar = positions[i];
                var arr = c[arrayVar]
                for(index in arr) {
                var item = arr[index];
                // ...
                }
                ...
                [color=blue]
                > I believe the eval function from Vincent does the trick...[/color]

                "eval" is almost never the best solution. It is inefficient and
                errorprone, and should be left for the few cases where it can't be
                avoided.
                [color=blue]
                > "Ivo" <no@thank.you > schreef in bericht
                > news:40fbcac4$0 $62721$ee9da40f @news.wanadoo.n l...[/color]

                Please don't top post.

                Regards
                /L
                --
                Lasse Reichstein Nielsen - lrn@hotpop.com
                DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
                'Faith without judgement merely degrades the spirit divine.'

                Comment

                • Lasse Reichstein Nielsen

                  #9
                  Re: address array by variable variable

                  "Quarco" <*DONT*[mps]_YOU#DARE(SEND) SPAM@@[webmind]--TOME.[.nl]%%^.orgtext> writes:

                  On second thought, this solution is even prettier:

                  ---
                  var liggend = [
                  ["value1a","valu e2a","value3a", "value4a","valu e5a"],
                  ["value1b","valu e2b","value3b", "value4b","valu e5b"],
                  ...
                  ];
                  var staand = [
                  ["value1c","valu e2c","value3c", "value4c","valu e5c"],
                  ...
                  ];

                  var positions = [liggend, staand, staand, liggend, staand, staand, liggend];

                  function getValue(i) {
                  var arr = positions[i];
                  for (var index in arr) {
                  var item = arr[index];
                  ...
                  }
                  ...
                  ---

                  Good luck.
                  /L
                  --
                  Lasse Reichstein Nielsen - lrn@hotpop.com
                  DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
                  'Faith without judgement merely degrades the spirit divine.'

                  Comment

                  • Quarco

                    #10
                    Re: address array by variable variable

                    Hmmm ..

                    Think this is the 'ultimate' solution Lasse... :-)

                    Thanx a lot!

                    Marco

                    "Lasse Reichstein Nielsen" <lrn@hotpop.com > schreef in bericht
                    news:658ju9lc.f sf@hotpop.com.. .[color=blue]
                    > "Quarco"[/color]
                    <*DONT*[mps]_YOU#DARE(SEND) SPAM@@[webmind]--TOME.[.nl]%%^.orgtext> writes:[color=blue]
                    >
                    > On second thought, this solution is even prettier:
                    >
                    > ---
                    > var liggend = [
                    > ["value1a","valu e2a","value3a", "value4a","valu e5a"],
                    > ["value1b","valu e2b","value3b", "value4b","valu e5b"],
                    > ...
                    > ];
                    > var staand = [
                    > ["value1c","valu e2c","value3c", "value4c","valu e5c"],
                    > ...
                    > ];
                    >
                    > var positions = [liggend, staand, staand, liggend, staand, staand,[/color]
                    liggend];[color=blue]
                    >
                    > function getValue(i) {
                    > var arr = positions[i];
                    > for (var index in arr) {
                    > var item = arr[index];
                    > ...
                    > }
                    > ...
                    > ---
                    >
                    > Good luck.
                    > /L
                    > --
                    > Lasse Reichstein Nielsen - lrn@hotpop.com
                    > DHTML Death Colors:[/color]
                    <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>[color=blue]
                    > 'Faith without judgement merely degrades the spirit divine.'[/color]


                    Comment

                    Working...