10 Replies Latest reply on Feb 5, 2007 11:41 PM by kenji776

    Dynamic Variable Names from an Array

    kenji776
      Hello everyone.
      I am working on a semi complex project that requires me to be able to create variables on the fly. Basically I have an array, the second element contains the name of the variable, and the 3rd element contains the data. I wanted to do something like what I have posted below, but it's not working. I should get fields named like form.a, form.b, form.c and so on, each with some data assigned, but when dumped, the form variables remain empty.Like form.finalarray[1][2] = "#finalArray[1][3]#" should come out to be an form.a = c, but when form.a is dumped, it is empty. Why doesn't the block of code below work?

        • 1. Re: Dynamic Variable Names from an Array
          insuractive Level 3
          You'll want to use the syntax:
          <cfset form[finalarray[1][2]] = "#finalArray[1][3]#">
          to correctly set the form variable
          • 2. Re: Dynamic Variable Names from an Array
            Fernis Level 3
            FORM is a basically ColdFusion structure, form field values as structure keys.

            You can only pass simple values in form fields. Thus it makes no sense being able to have arrays as form field variables. Thus, I don't see why your code should work...

            If you create a myStruct structure and replace "form." with "mystruct.", does your code work then?

            Btw, you gain nothing by using "#finalArray[1][3]#", compared to finalArray[1][3]. Don't use # signs or quotes in your code example, they are bad coding practice.

            Edited: And yes ,see the answer above this reply. There the form key value is actually evaluated, because the key name finalArray[x][y] is wrapped within the []'s. It's the same as form['#finalArray[x][y]#'] if that makes it clearer to you. But again,ditch the # and "s.
            • 3. Re: Dynamic Variable Names from an Array
              Level 7
              > Basically I have an array, the second element contains
              > the name of the variable, and the 3rd element contains the data.

              What does the FIRST element contain?

              And how come you're not using an array of structures, as there seems to be
              more a key/value relationship here, rather than an indexed one, which
              suggests some numeric signficance in the order of the elements (which there
              isn't, between "name" and "value" which seems to be your siutation).

              --
              Adam
              • 4. Re: Dynamic Variable Names from an Array
                kenji776 Level 1
                Thank you all so much for your replies. I know this peice of code looks insane and like it serves no prupose, but trust me it does. The first element of the array conaints a number, that frequency of how many times the letter in element 2 occures in a block of text in the form. For those interested, the project I am working on is a tool that will help the cracking of monoalphebetic substitution ciphers using frequency analysis. That array is what holds the frequencies of every letter in the block of text given. It gives the freq in the first element, the actual letter in the second element, and the letter it probably originally was in the 3rd element. I am trying to fill in some form fields with the data from the array. Again, I know this all sounds insane, but there is method to the madmess. If you care to look, this project in progress can be found at http://www.digitalswordsmen.com/Codebreaker.cfm. Its free to fool around with. Lots of diagnostic info is displayed, and the source is downloadable if you want to check it out. Thanks again for all your help, Ill let you know how it goes.
                • 5. Re: Dynamic Variable Names from an Array
                  Level 7
                  > It gives the freq in
                  > the first element, the actual letter in the second element, and the letter it
                  > probably originally was in the 3rd element.

                  Right, so you're describing a struct, not an array. There is no
                  *sequential* relationship between "freq", "cipher letter", and "probable
                  letter", is there? So an indexed array is not the most sensible/obvious
                  data construct to store the data in.

                  What's more, you've got uniform key names, right there:

                  stChar.freq
                  stChar.cipher
                  stChar.actual

                  Although as you have a collection of them, rejog that slightly:

                  stChars[cipher].freq
                  stChars[cipher].actual

                  (given the cipher chars would be unique, I should think, in your system).

                  Did you suss out your problem with the AAAAAA thing, from last night? I
                  suspect I saw what the problem was, but wondered if a bit of
                  experimentation would lead you to the same conclusion I did... [plus it was
                  late when I saw the thread, and I'd want to run some test code to check my
                  theory, and I... well... couldn't be bothered, to be honest. Oops].

                  --
                  Adam
                  • 6. Re: Dynamic Variable Names from an Array
                    kenji776 Level 1
                    Ah. Yeah. See I am a really terrible programmer, and I don't even know the difference between a structure and array. I don't even know how I have made as much as I have. The AAAAA problem is partially fixed, sometimes it still freaks out and puts a bunch of one letter for no good reason that I can think of. If you know why, please let me hear it. I wish i was better at coding.
                    • 7. Dynamic Variable Names from an Array
                      kenji776 Level 1
                      Oh yeah also that fix provided worked for my dynamic vars. Now I just need to save them back to the form for the replacement alphabet. So thanks for that.

                      Well After some work, I got it going. It puts the correct values in the correct form boxes. However my replace statements are messed up. I think its because one replace statement is change another. I need some way to do them all at the same time. Basically I just want to change every letter in "Text" to whatever values the user has specified in the form. So whatever they put for Form.A should replace any A that occurs in the text. Attached is the code for my replace statements.

                      This image here http://www.digitalswordsmen.com/img/cipher_tool_output.gif Really shows what is going on better than i can explain it. Everything is all set up right, but the results of the replace are completely unpredictable.
                      • 8. Re: Dynamic Variable Names from an Array
                        Level 7
                        > The AAAAA problem is partially fixed, sometimes it still freaks
                        > out and puts a bunch of one letter for no good reason that I can think of.

                        Let's take a smaller sample, in which the alphabet is only three
                        characters, ABC.

                        Your cipher mapping is:
                        C->A
                        A->B
                        B->C

                        Now, decipher "CAB":

                        Change the Cs to As:
                        AAB

                        Change the As to Bs:
                        BBB

                        Change the Bs to Cs:
                        CCC

                        That approach is never going to work, is it? You're always going to
                        accidentally decipher some letters more than once.

                        > If
                        > you know why, please let me hear it. I wish i was better at coding.

                        This ia more a debugging issue. Had you ever actually manually followed
                        your logic through (with pen and paper, if need be, to track results),
                        you'd've seen this. When something is wrong, don't sit there going "it
                        SEEMS right, I can't see it", have the point of view: "it's clearly
                        wrong... why?".

                        All this comes with practice.

                        --
                        Adam
                        • 9. Re: Dynamic Variable Names from an Array
                          Level 7
                          > That approach is never going to work, is it? You're always going to
                          > accidentally decipher some letters more than once.

                          If it's any consolation, CF falls into the same trap:

                          <cfoutput>#replaceList("cab", "c,a,b", "a,b,c")#</cfoutput>

                          (outputs "ccc").

                          --
                          Adam
                          • 10. Dynamic Variable Names from an Array
                            kenji776 Level 1
                            Ha, I found a solution. I use a case sensitive replacement. Before replacement I set the variable to uppercase, then I replace with lowercase letters. That way nothing gets replaced more than once, because it will always change case! It seems to work perfectly. The whole thing works now, if you get a change you should check it out. Thanks for everyones help. If you care to try for yourself, here is the code. It's not to shabby if I do say so myself.