Skip to main content
September 17, 2010
Answered

trouble assigning array values in cfscript function

  • September 17, 2010
  • 2 replies
  • 1866 views

I'm trying to pass an array to a cfscript function and assign values to it. When I see if the value was assigned right, I get an error saying that I'm trying to dereference a scalar.

            function g(q,recsq,arr) // q is a query name, recsq is the number of rows in q,arr is an array name

            {

               evaluate(arr&'[1][1]='&q&'.code[1]');
               writeoutput(a[1][1]);
            }
            g('qpr','#recsqpr#','apr');

The assigning already works if I don't try to call a function but I would like to know what I am doing wrong.

    This topic has been closed for replies.
    Correct answer Adam Cameron.

    Sorry for taking so long.

    Had trouble passing array in so yes, I passed the name I wanted it to have. In the last line, apr[1][1] shows up right so think this works.

    Seems like could be simpler with no evaluate()s. Would also like to pass in array and not just name.

                function g(q,arr)

                {

                   evaluate(arr&'=arraynew(2)');

                   upto=evaluate(q&'.recordcount');

                   for (ii=1;ii lte upto;ii=ii+1)

                   {

                      evaluate(arr&'[ii][1]='&q&'.code[ii]');

                      evaluate(arr&'[ii][2]='&q&'.descriptor[ii]');

                      writeoutput(evaluate(arr&'[ii][1]'));

                   };

                }

                g('qpr','apr');

                writeoutput(apr[1][1]);


    What "trouble" did you have passing the array in?

    There's no difference passing an array as a function param to any other sort of data type:


    myArray = arrayNew();
    myArray[1] = "something";
    myArray[2] = "something else";


    myArray = myFunction(myArray);


    writeOutput("Here's the new value: #myArray[3]#");



    function myFunction(someArray){
        arrayAppend(someArray, "some new value");
       
        return someArray;
    }

    Do you understand how that all works?

    --

    Adam

    2 replies

    Inspiring
    September 17, 2010

    '&q&'.  looks like a simple string to me.

    September 17, 2010

    It is a simple string for evaluate(). Trying to do without evaluate()s.

    Inspiring
    September 17, 2010

    I notice in your function, you don't specify the datatypes for your arguments.  What happens if you do something really basic like dump them (assuming you can dump in cfscript).

    Inspiring
    September 17, 2010

    OK, first rule of thumb: if you ever find yourself wondering if evaluate() is going to be part of your answer to

    any issue, the a nswer is almost certainly "no".


    One does array assignment in CFScript exactly the same way one does it in a <cfset> statement, eg:

    a[...] = someValue;

    (where [...] suggests however many dimensions you have).

    In your example I cannot see why it would not be:

    arr[1][1] = q.code[1];

    Although it seems a bit odd that you're hardcoding [1][1] there, but perhaps there's more to this than you're letting on...

    --

    Adam

    September 17, 2010

    Thank you for the reply.

    Was trying with 1 value to test. Tried just assigning with arr[1][1]='a' and got the same error. Also tried putting array name apr[1][1] without passing as parameter and got same error. Was using evaluate() before since arr and q were passed in as strings 'apr' and 'qpr'.

    Inspiring
    September 17, 2010

    Just to get this straight, you had this line of code:

    arr[1][1]='a';

    And it errored with:

    trying to dereference a scalar [variable]

    ?

    That says to me that either arr or arr[1] are strings, not arrays.  To treat them as arrays, they need to be arrays, eg:

    arr = arrayNew(1);

    arr[1] = arrayNew(1);

    or

    arr = arrayNew(2);

    has to precede any references to arr being an array.

    --

    Adam