2 Replies Latest reply on Jul 18, 2007 6:17 AM by Davide_C

    how to use string concatenation to get property object

    Davide_C Level 1
      Hi, I'm an ActionScript newbie, and I would like to ask you a very simple question because this problem is making me crazy.
      This is a simplified part of my script:

      Dati_colori = new LoadVars();
      Dati_colori.load("immagini/colori/"+_root.num_img+".txt");

      The file is this:

      &num=3&colore1=0xFF0000&colore2=0x00FF00&colore3=0x0000FF

      The first variable is the number of colors (that are the other variables)
      After the file is loades, I can access to the values by writing:

      Dati_colori.colore1, Dati_colori.colore2, ecc ecc.

      Now the problem: I would like to put this in a for cicle:

      colors is an array containing the values

      for (a=1;a<Dati_colori.num<a++){
      colors[a] = Dati_colori.colore+a //this concatenation DOES NOT WORK!! I cannot access this way to the property containing the
      }

      Maybe it is just a formal problem but I cannot succeed in using string concatenation to get the property value.

      Thanks in advance for any suggestion!

      Davide (from Switzerland)
        • 1. Re: how to use string concatenation to get property object
          Level 7
          Davide,

          > for (a=1;a<Dati_colori.num<a++){
          > colors[a] = Dati_colori.colore+a //this concatenation DOES NOT WORK!! I
          > cannot access this way to the property containing the
          > }

          You need more than just concatentation for something like this,
          otherwise, at best, you will only be setting elements in your colors array
          to a series of strings. What you want are object references, and you can
          either use the eval() function to evaluate those strings as variables or use
          the array access operator, which works for more than just arrays.

          for (a=1; a<Dati_colori.num; a++) {
          colors[a] = Dati_colori["colore"+a]
          }

          Just as an aside, note that I changed the second "<" character in your
          for() loop to a semicolon (semicolons separate the three portions inside the
          parentheses). Verify that you really want the variable "a" to be set to 1,
          rather than 0 (you may already have a value in the first [zero] slot, but
          arrays do start at zero). You might, for example, actually want something
          like this ...

          colors[a] = Dati_colori["colore"+a+1]

          ... if "a" is initially set to zero.

          The key is the omitted dot (.) after Dati_calori, which is replaced with
          the array access operator ([]), which contains the string "colore"
          concatenated with the value of a.


          David Stiller
          Co-author, Foundation Flash CS3 for Designers
          http://tinyurl.com/2k29mj
          "Luck is the residue of good design."


          • 2. Re: how to use string concatenation to get property object
            Davide_C Level 1
            THANK YOU VERY MUCH, David!!

            Davide