What is the simpliest way to use the name of an array that has been passed as an argument to a function

Hi to all,

this is another OOP question: what would be the best and simpliest way to use/get/return the name of an specific array that has been passed to a function, e.g in the following simple example.

<cfscript>
public struct function someFunctionName( array someArray required){
local.result={}; 
local.result["arrayname"]="here should go the name of the array that has been passed to argument.someArray"; 
local.result["arraycontainer"]=argument.someArray;
return local.result;
}
</cfscript>

I could pass the name as an argument, but I’d like to have it more dynamic.

Thanks.

@andreas, what you mean by the dynamic value in this place? can you give a bit more elaborate this?

I couldn’t sure. Did you mean this?
We can return the name of an array if we use the return format array as the above example.

If you didn’t mean this means, let here know as I said first :slightly_smiling_face:

Thanks for asking and taking time for my question @cfmitrah. I appreciate it a lot. I don’t want to get too much into details, because it would take more time to explain what I am using it for. But I have a very simple usecase to describe the issue quite perfectly.

Let’s say I want to output values of an array as html and want to use the name of the passed array to add it as a comment to that html, e.g.:

<cfscript>
public string function outputArrayAsHTML(  array someArray required ){
  local.htmlContentStart="<!--START:" & "theNameOfthePassedArray" & "--><div>";
  local.htmlContent=arguments.someArray.toList( "</div><div>");
  local.htmlContentEnd="</div><!-- END:" & "theNameOfthePassedArray" & "-->";
  return local.htmlContentStart & local.htmlContent & local.htmlContentEnd;
}
userNamesArray=[  "Mike", "Charlie", "Juliet", "Oscar", "Romeo" ];
echo( outputArrayAsHTML( userNamesArray ) );
</cfscript>

The function above should output the following HTML (note that here the comment doesn’t contain the string “theNameOfThePassedArray”, instead it uses the name of the array “userNamesArray”:

<!--START: userNamesArray --><div>mike</div><div>charlie</div><div>juliet</div><div>Oskar</div><div>Romeo</div><!--END: userNamesArray -->

I know I could simply change that function to add a second argument e.g. outputArrayAsHTML( userNamesArray, “userNamesArray” ) and use that string for the comment. But it would be nice to keep the function a monadic method.

That’s all.

1 Like

You could do it with an ordered struct, though still passing the name of the array, at least it’s only the one argument required.

'bob' = [
    'jane' = [ 'sue', 'lucy', 'mark' ]
];

writeDump( bob );

To my knowledge, there is no way of knowing what the variable name of the array was before it is passed to a function. There is probably some way of getting it from the calling request context by diving into Lucee core functions… and maybe one of the lucee devs can speak to that, but an ordered struct gives you what you need as a workaround.

EDIT: I guess, technically speaking, you don’t even need the ordered struct. A regular struct would also do:

'bob' = {
    'jane' = [ 'sue', 'lucy', 'mark' ]
};

HTH

– Denny

1 Like

Thanks @ddspringle!!!
After looking at your answer and googling a little bit deeper about my question and also into other languages than cfml and java, I came to the conclusion that a solution for my question is not possible. Maybe somebody might correct me in the future.

The cause is: it looks like the passed variables get evaluated at runtime and the names don’t get passed to the function itself.

Considering that my function above can also be invoked by simply calling it like so makes it clearer:

echo( outputArrayAsHTML( [  "Mike", "Charlie", "Juliet", "Oscar", "Romeo" ] ) );

There even isn’t a array name being passed, just the array values itself. That is how the function gets called at runtime. When deciding to use a workaround by additionally adding the variable name as a string vs. adding it as a string in a second argument to the function, I’d use the latter.

Again, thanks for helping me to decide this one and taking your time for a possible solution!

@andreas You are correct. In the heap, the array itself has no name associated with it. The array is simply a chunk of memory allocated to hold data. Then, variables in your code are simply pointers to that chunk of memory. There can be as many pointers as you want to the same array, or none at all. So if there is additional metadata about an array that you want your function to know about it, it needs to be passed explicitly in the function arguments.

1 Like