Owain...
Perhaps it's best you don't help the OP after all. He might become jaded and self-important like yourself. Why bother coming to a forum where people are asking questions if you're not going to answer.
Telling someone to RTFM as your first comment to them is NEVER acceptable. Not only is it just plain rude it's not helpful either.
Andy, when you've spent as much time helping people on these forums as much as Owain has, then perhaps you might have a basis for whining at him. Until that time: your sort of input into this thread is as invalid and pointless as his. You yourself have pretty much engaged in exactly the same sort of posting as Owain has, in this instance, except not having first earned the respect that Owain already has on these forums.
So, Andy, stick around for a coupla years and help people every day, and then get back to us as to who should and should not make comments, and pronounce who is and is not helpful. You might also then understand precisely where Owain was coming from with his post.
That said: not your most helpful post ever, Owain ;-)
--
Adam
Phil, I think demonstrating how one can loop over scopes is handy info for Rockit8 here, but your example code is pretty dire. Basically what it does is take a simple value (arguments.simpleVar), and returns the name of any old variable in the searched scopes which has the same value. In what way is that:
a) useful;
b) any sort of legitimate answer to the OP's original question?
Consider this usage of your code:
<cfscript>
request.theOneIWant = true;
variables.notTheOneIWant = true;
// get variable stats (see function above)
varStats = getSimpleVariableStats(request.theOneIWant, [variables,request]);
// output your string with variable stats
writeOutput("The value of " & varStats.variableName & " is " & varStats.value);
</cfscript>
This outputs:
The value of NOTTHEONEIWANT is true
In what way is that helpful?
And a tip: if you want an array of the keys of a struct, you can simply use structKeyArray(). You don't have to use a combination of structKeyList() and listToArray(). And in your particular situation, you could have simply looped over the struct directly with for (key in collection). This would have simplified your example code somewhat, removing unnecessary clutter (which is always a good thing, when trying to focus someone on something with example code).
I continue to maintain the question as asked was not sensible, but this is the best response yet in demonstrating some techniques for accessing variables when one doesn't necessarily know a variable's name.
But to do what the OP actually asked, one needs to know the variable's name already. Which renders-pointless the exercise of having a function to return its name.
However I think I've actually worked out what they were asking now, and will reply to their original post directly in a mo'
--
Adam
Hi Rockit8.
I know you've worked around your issue (I'd still like to know exactly what it was you needed to do), but I think I have worked out kinda what you were asking, and to quench my curiousity, I want to follow this up some more to clarify some things (for me, and possibly for you).
I suspect what you're asking is demonstrated by this pseudocode:
<cffunction name="myFunction">
<cfargument name="theArgumentFromThePerspectiveOfTheFunction">
<!---I have arguments.theArgumentFromThePerspectiveOfTheFunction, but I want to know that it got its value from variables.theVariableWhoseNameIWantToKnow --->
<cfreturn somehow("variables.theVariableWhoseNameIWantToKnow")>
</cffunction>
<cfset variables.theVariableWhoseNameIWantToKnow = "some value">
<cfset variables.theNameOfTheVariable = myFunction(variables.theVariableWhoseNameIWantToKnow)>
Is that the sort of thing you're wanting to do? This is not possible.
When calling a function like this, the function never knows anything about variables.theVariableWhoseNameIWantToKnow, it never gets passed variables.theVariableWhoseNameIWantToKnow, it just gets passed its value. And CF puts the value into arguments.theArgumentFromThePerspectiveOfTheFunction. The only variable myFunction() knows about is arguments.theArgumentFromThePerspectiveOfTheFunction. It is irrelevant to myFunction() that the calling code used a variable to hold "some value", it could just as likely have been:
<cfset variables.theNameOfTheVariable = myFunction("some value")>
This demonstrates why I say your question - as asked - is meaningless.
However not all data is passed by value. Consider this code:
<cfset variables.firstOneToReferenceTheStruct = {foo="bar"}>
<cfset variables.anotherOneThatReferencesTheStruct = variables.firstOneToReferenceTheStruct>
In CF, a struct is copied by reference (kinda), not value, so both variables.firstOneToReferenceTheStruct and variables.anotherOneThatReferencesTheStruct will be separate references to the same piece of memory (and the memory holds the struct {foo="bar"}.
So if one has this code:
<cfset variables.theNameOfTheVariable = myFunction(variables.anotherOneThatReferencesTheStruct)>
Then arguments.theArgumentFromThePerspectiveOfTheFunction will simply be yet another reference to exactly the same memory as variables.firstOneToReferenceTheStruct and variables.anotherOneThatReferencesTheStruct. So one might initially think "aha, so there's a way to know that arguments.theArgumentFromThePerspectiveOfTheFunction was 'originally' called 'variables.firstOneToReferenceTheStruct'". No, there isn't. Because whilst those references all point to the same memory, they're all still completely separate references (a reference is a memory address, so each of those three actually just hold the memory address that the struct is at, but it's three separate copies of the address. Not three separate copies of the content of the memory address, but three separate copies of the address), and do not have any corelation at all.
Now... Java obviously keeps track of which references point to which bits of memory (so it knows when memory can be cleared by the garbage collector), so it is possible to find out a list of references that reference that particular piece of memory, but I don't know that this is exposed even via the Java API, and it's definitely not exposed to ColdFusion. I'm not a Java developer, but I can google, but I did not come up with any Java functionality that exposes this (disclosure: I only spent about 2min trying). I suspect it's too low level, and not really of any particular use. Why really would one want to know this sort of thing anyhow? It's not useful information to an application, after all.
Does this explain why I said your question is a bit meaningless?
--
Adam
@Adam,
You are wrong about Andy's commitment to this forum and the community, you probably noticed that he has the Community Professional badge. This badge is given to folks who have a track record of doing exactly the opposite of what you accused him of. Andy was right in calling Owain out and I commend him for it. No one here, regardless of how much time they spend helping out, has the right to gauge a question's's worth in a SUPPORT forum! If you feel you don't want to help a person for whatever reason, just close the topic and move on.. don't perch up high in some non-existent pedestal and talk down to someone asking for help... for whatever reason, it's just rude and unprofessional!
As to my answer to the @Rockit8's question, he was looking for a way to dynamically figure out a given variable's name, as stated in the question:
"I needed something like writeoutput("The value of" & function_to_get_var_name(myvar) & " is " & #myvar#);"
The code was written in literally one minute to point him in the right direction, notice the hint on the function that the first occurence value found would be returned so obviously the example you provided would return the undesired result.
Phill Nacelli wrote:
... for example, if you set on a .cfm page:
myVariable = "myValue";
The myVariable is being set on the VARIABLES scope. meaning it's now VARIABLES.myVariable. If you think in these terms you'll probably come to the realization that you can search the appropriate scope and extract the piece of information you need, here's an example for your problem:
/*
* searches scopes for a variable's name and value, returns a struct with variable's name and value
*/
Quite inspired, honestly.
Adam, thanks for your reply.
What you described was equivalent to what i wanted to do in the first place, but just to clarify i will explain it here.
I had to develop a custom tag where i would receive a coldfusion array, and return a structure with a property with the name of that array...so i somehow needed to know the name of the array inside my custom tag.
After reading the replys in this discussion i came to the conclusion that what i was asking might not be possible to do, so i worked my way around it by receiving the name of that array as an argument.
Thanks and regards.
North America
Europe, Middle East and Africa
Asia Pacific