11 Replies Latest reply on Feb 12, 2009 5:35 PM by safin_luo

    How to get value of variables which have dynamic name?

    safin_luo
      Hey guys,
      1.For example, I have two variables:
      <cfset varOne="One">
      <cfset varArg="argument">
      And I'd like to connect"argument" and "One" together as "argumentOne", then how can I get the value of "argumentOne"?

      Thanks!
        • 1. Re: How to get value of variables which have dynamic name?
          Level 7
          #variables[varArg & varOne]# works (preferred syntax)
          #evaluate(varArg & varOne)# does as well (not so preferred)

          Azadi Saryev
          Sabai-dee.com
          http://www.sabai-dee.com/
          • 3. Re: How to get value of variables which have dynamic name?
            safin_luo Level 1
            Hi,
            another question:
            I want to build a dynamic form and update a table with the form data
            I have a form named Form1 in a page A.cfm, and it post to B.cfm by click a submit button. B.cfm invoke a components called C.cfc, and pass Form1 as an argument to a function in C.cfc.
            here is the function code:
            <cffunction name="UpdateRule" returntype="void">
            <cfargument name="ruleFormData" type="struct" required="true">
            <cfset minText = "ruleFormData.minText_col">
            <cfset maxText = "ruleFormData.maxText_col">
            <cfloop query="qGetCol">
            <cfquery name="qUpdateCol" datasource="IMD_CARACAL">//qUpdateCol is a query which returns column number, like 1,2,3, col_id is one of its column
            UPDATE support_scratch.DA_MB_PTAV_RULE
            SET min_val = <cfqueryparam value="#variables[minText & col_id]#" cfsqltype="cf_sql_float">,
            max_val = <cfqueryparam value="#variables[maxText & col_id]#" cfsqltype="cf_sql_float">
            WHERE ptav_rule_id = <cfqueryparam value="#ruleFormData.rule_id_text#" cfsqltype="cf_sql_char">
            AND col_id = <cfqueryparam value="#qGetCol.col_id#" cfsqltype="cf_sql_integer">
            </cfquery>
            </cfloop>
            </cffunction>

            but when it runs, it throws an errow : Element ruleFormData.minText_col1 is undefined in a Java object of type class coldfusion.runtime.VariableScope.
            Could you please help on this issue?
            • 4. Re: How to get value of variables which have dynamic name?
              Dan Bracuk Level 5
              Troubleshooting step number 1 - look at what you sent to your function.

              <cffunction name="UpdateRule" returntype="void">
              <cfargument name="ruleFormData" type="struct" required="true">
              <cfdump var="#arguments#">
              <cfabort>

              Also, inside your function it's a good practice to use the keyword var when initializing local variables.

              Finally at some point, it looks like qGetCol looks like it is also undefined.
              • 5. Re: How to get value of variables which have dynamic name?
                safin_luo Level 1
                I've used <cfdump> tag and it is sure that the Form data is passed to the function. Here are some of the dump information:
                MAXTEXT_COL1 1100000000.00
                MINTEXT_COL1 800000010.00

                sorry that I forget to mention that qGetCol is defined in this function and I omitted it.
                It is really strange that I can get the value of "#ruleFormData.maxtext_col1#" but can't get it by "#variables[minText & col_id]#"..
                I don't know what caused this issue...
                • 6. Re: How to get value of variables which have dynamic name?
                  Dan Bracuk Level 5
                  What are you dumping? Your only argument is looking for a structure, so if you dump the arguments, you should see a structure somewhere. Do you? Does it include ruleFormData.minText_col1 ?

                  By the way, where are you trying to use it? I didn't see anything resembling that in your post of 02/12/2009 12:16:08 PM
                  • 7. Re: How to get value of variables which have dynamic   name?
                    Level 7
                    inside a CFC function, you can't use VARIABLES prefix to reference local
                    variables.

                    as Dan already suggested, you should create variables local to the
                    function using VAR keyword, as in <cfset var myvariable = "something">.

                    if i 'get' your code correctly, you need to actually reference ARGUMENTS
                    scope, if any, instead of VARIABLES, i.e.:

                    #arguments[mintext & col_id]#

                    (which, from what i gather, would evaluate to
                    arguments.ruleFormData.minText_colX)

                    but i have a sinking suspicion that the way you are going about whatever
                    you are doing is just not right... sorry i do not have more time right
                    now to look deeper into it, but one thing that jumps out at me is that
                    you are passing a form structure to your function - why do not you juist
                    loop over that structure and get the keys you need to use from it? why
                    do you need to create new variables to be able to reference and evaluate
                    other variables? that just does not seem right...

                    Azadi Saryev
                    Sabai-dee.com
                    http://www.sabai-dee.com/
                    • 8. Re: How to get value of variables which have dynamic name?
                      safin_luo Level 1
                      yes, I've dumped the arguments and see a structure contains the form data, it includes ruleFormData.minText_col1
                      Now it works after I use #arguments[mintext & col_id]# instead of #variables[mintext & col_id]# as Azadi's suggestion.
                      Thank you!
                      • 9. Re: How to get value of variables which have dynamic   name?
                        Level 7
                        yes, I've dumped the arguments and see a structure contains the form data, it
                        includes ruleFormData.minText_col1
                        Now it works after I use #arguments[mintext & col_id]# instead of
                        #variables[mintext & col_id]# as Azadi's suggestion.
                        Thank you!

                        • 10. Re: How to get value of variables which have dynamic   name?
                          safin_luo Level 1
                          The code can run successfully after I reference arguments. Because I have a form that contains some dynamic components, the name of them determined by the col_id, I just want to get the value of them to update the table. It is the best method I know so far. Thank you very much:)
                          • 11. Re: How to get value of variables which have dynamic   name?
                            safin_luo Level 1
                            The code can run successfully after I reference arguments. Because I have a form that contains some dynamic components, the name of them determined by the col_id, I just want to get the value of them to update the table. It is the best method I know so far. Thank you very much:)