Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

trouble assigning array values in cfscript function

Guest
Sep 17, 2010 Sep 17, 2010

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.

1.6K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Sep 18, 2010 Sep 18, 2010

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

Translate
LEGEND ,
Sep 17, 2010 Sep 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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Sep 17, 2010 Sep 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'.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 17, 2010 Sep 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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Sep 17, 2010 Sep 17, 2010

Thanks. THey are strings but I was hoping evaluate() would work.

I will try declaring the array inside the function instead of outside which I am doing now. Will let you know.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 17, 2010 Sep 17, 2010

If they are arrays "outside" the call, why are you not just passing the array itself in, rather than - if I understand what you mean - some string containing the... what... name of the array?

Can you post a complete code block demonstrating the calling code and the function?

--

Adam

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Sep 17, 2010 Sep 17, 2010

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]);

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 18, 2010 Sep 18, 2010

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Sep 20, 2010 Sep 20, 2010

The problem was that the [1][2] element could not be found.

I understand how it works.

With the return at the end of the function and calling it like apr2=try(apr2) instead of no return in the function and calling it like try(apr2), it worked.

Thank yo very much.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Oct 08, 2010 Oct 08, 2010
LATEST

Yes you should totally be able to do something along these lines:

<cfscript>      function someFunction(qry, array)      {           var upTo = qry.recordCount;           var ii = 0;                      array = arrayNew(2);                      for (ii = 1; ii LTE upTo; ii = ii + 1)           {                array[ii][1] = qry.code[ii];                array[ii][2] = qry.descriptor[ii];                writeOutput(array[ii][1]);           }      }       </cfscript> <cfoutput>            <cfquery name="qry" datasource="someDatasource">           SELECT code, descriptor FROM someTable      </cfquery>            <cfset newArray = arrayNew(2) />      <cfset someFunction(qry, newArray) />       </cfoutput>

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 17, 2010 Sep 17, 2010

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Sep 17, 2010 Sep 17, 2010

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 17, 2010 Sep 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).

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Sep 17, 2010 Sep 17, 2010

The examples I've seen on the internet about passing parameters in cfscript didn't have datatypes specified.

Found a few articles on dumping variables so seems to mean your assumption about dumping in cfscript is right. So far can't get to it work.

Sorry don't understand where your question leads to, please let me know.

The function I have works but I want to do it without evaluate()s.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 17, 2010 Sep 17, 2010

http://www.anujgakhar.com/2007/12/28/using-dump-within-cfscript/ has a code sample to dump in cfscript.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources