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

Var name as string

Guest
Apr 13, 2012 Apr 13, 2012

Copy link to clipboard

Copied

Hi,

I'm new to cold fusion and i'd like to know how can i get a variable name to a string or other variable...

I want to declare a variable:

myvar = "contents";

and dinamically create an ouput like this: "the value of myvar is "contents""

Thanks

Views

3.4K

Translate

Translate

Report

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 , Apr 14, 2012 Apr 14, 2012

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.theArgumentFr

...

Votes

Translate

Translate
Guide ,
Apr 13, 2012 Apr 13, 2012

Copy link to clipboard

Copied

I really think you should buy a book or read the docs and examples online, these forums aren't just free tutorials.

Votes

Translate

Translate

Report

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
Apr 13, 2012 Apr 13, 2012

Copy link to clipboard

Copied

I searched on google and didnt find anything usefull.

I know that this might be simple stuff but i wasnt able to find anything about it.

I can only get the name of properties in a structure, if you know some usefull article about this i'd apreciate it.

Votes

Translate

Translate

Report

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
Participant ,
Apr 13, 2012 Apr 13, 2012

Copy link to clipboard

Copied

Hey Owain...

Don't be a jacka$$.

These forums ARE free tutorials...that's the whole point. If someone doesn't know how to do something in ColdFusion these forums should be the FIRST place they come to. Google's nice but Adobe forums should be the fount of all ColdFusion wisdom.

Votes

Translate

Translate

Report

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
Guide ,
Apr 13, 2012 Apr 13, 2012

Copy link to clipboard

Copied

No, these forums are a place for people to give up their free time to help other people when they've exhausted other options, you cannot expect people to jump to your aid if you can't even be bothered to search about for an answer or state what you've already discovered.

Coming on here as a first point of call just shows laziness, nothing else. I've spent hours of my time on here for free giving apparently 1200 replies to peoples questions, as well as years reading books and tutorials to get the skills I have, so you'll excuse me if I don't take your suggestion on board.

Votes

Translate

Translate

Report

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
Participant ,
Apr 13, 2012 Apr 13, 2012

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

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 ,
Apr 14, 2012 Apr 14, 2012

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

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
Community Beginner ,
Apr 14, 2012 Apr 14, 2012

Copy link to clipboard

Copied

@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.

Votes

Translate

Translate

Report

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
Community Expert ,
Apr 13, 2012 Apr 13, 2012

Copy link to clipboard

Copied

I want to declare a variable:

myvar = "contents";

and dinamically create an ouput like this: "the value of myvar is "contents""

Arguably, the standard way in any language to dynamically manipulate a given parameter is by means of a function. The following example should give you a flavour.

<cfscript>

myvar = "contents";

/* Call the function */

doSomething(myvar);

function doSomething (someParam) {

writeOutput("The value of myvar is " & "'" & someParam & "'");

}

</cfscript>

Votes

Translate

Translate

Report

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
Apr 13, 2012 Apr 13, 2012

Copy link to clipboard

Copied

I can do that, but that is static... you hardcoded "the value of myvar is"...

I needed something like writeoutput("The value of" & function_to_get_var_name(myvar) & " is " & #myvar#);

Votes

Translate

Translate

Report

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
Community Expert ,
Apr 13, 2012 Apr 13, 2012

Copy link to clipboard

Copied

Rockit8 wrote:

I can do that, but that is static... you hardcoded "the value of myvar is"...

I needed something like writeoutput("The value of" & function_to_get_var_name(myvar) & " is " & #myvar#);

I would disagree. The 'myvar' in 'The value of myvar is...' is just text. I might as well have written "The value of my variable is...". In any case, by your own definition, you, too have indeed hardcoded! Your example does more or less what I did. But I understand what you mean.

The main thing is the principle, which is, to use a function. We can easily modify the above code to make it more dynamic. It's just a matter of separating static from dynamic parts. For example, the display is done by the caller of the function, not by the function itself.

<cfscript>

myvar = "contents";

/* Call the functions */

writeoutput("The value of " & "'" & function_to_get_var_name(myvar)  & "'" & " after it undergoes transformation is " & doSomething(myvar));

function function_to_get_var_name (someParam) {

return someParam;

}

function doSomething (someOtherParam) {

return "***" & someOtherParam & "***";

}

</cfscript>

Votes

Translate

Translate

Report

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 ,
Apr 13, 2012 Apr 13, 2012

Copy link to clipboard

Copied

Let's think through what you're asking.  How can you identify which variable's name you wish to resolve without referring it at some point?  You need to know the name of a variable to do anything with it.

What you're asking doesn't make sense.

Take a step back... what is it you're trying to solve by thinking this is the way to solve it (if that makes sense)?

--

Adam

Votes

Translate

Translate

Report

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
Apr 13, 2012 Apr 13, 2012

Copy link to clipboard

Copied

I needed to dynamicaly create a property in a structure with the same name of a variable but inside the scope of a function it just does not make any sense because i would allways get the name of the function paramenter instead of the actual variable name...outside that scope i declare de variable so i know the variable name.

Something like this:

myvar = "contents";

obj = structnew();

name = function_to_get_var_name(myvar);

"obj.#name#" = arraynew(1);

After analysing a the problem a bit more i found other solution in the context of my problem.

Thanks for the replys.

Votes

Translate

Translate

Report

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 ,
Apr 13, 2012 Apr 13, 2012

Copy link to clipboard

Copied

name = function_to_get_var_name(myvar);

This demonstrates what I meant... to call function_to_get_var_name(), you need to code the very thing you want to return., eg: your code needs to specify myvar, and all the function is going to return is "myvar".  IE: you already need to know the answer to your question before you ask it.

After analysing a the problem a bit more i found other solution in the context of my problem.

Well that's good 🙂

--

Adam

Votes

Translate

Translate

Report

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
Community Expert ,
Apr 13, 2012 Apr 13, 2012

Copy link to clipboard

Copied

Rockit8 wrote:

"obj.#name#" = arraynew(1);

If you're going for elegance,

    

     obj[name] = arraynew(1);

Votes

Translate

Translate

Report

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
Community Beginner ,
Apr 13, 2012 Apr 13, 2012

Copy link to clipboard

Copied

@Rockit8, first of all, welcome to the ColdFusion Community, don't hesitate to ask any question on this forum or on Twitter using the hashtag #ColdFusion.  Our community is full of great folks ready to lend a hand.

Here's an example you can do to dynamically extract the information you want, keep in mind that all variables in coldfusion are set within a scope, 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

    * scope default to variables, you can pass an array of the scopes you want to search for given variable

    * first found occurrence is returned

   */

  public Struct function getSimpleVariableStats(required any simpleVar, array searchScopes=[variables]){

    // search across all passed scopes

    for(var scope in arguments.searchScopes)

    {

      // search across current scope's keys

      var scopeKeys = listToArray(structKeyList(scope));

      for(var scopeKey in scopeKeys){

        // if value is simple and found, return stats

        if(isSimpleValue(scope[scopeKey]) && scope[scopeKey] == arguments.simpleVar){

          return {

            "variableName" = scopeKey,

            "value" = scope[scopeKey]

          };

        }

      }

    }

  }

  // your variable

  someVariable = "someValue";

  // get variable stats (see function above)

  varStats = getSimpleVariableStats(someVariable);

 

  // output your string with variable stats

  writeOutput("The value of " & varStats.variableName & " is " & varStats.value);

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Hope this helps you, and best of luck.. like Andy said this forum is here to help anyone.  Hope to see you around!

Cheers..

Votes

Translate

Translate

Report

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
Apr 13, 2012 Apr 13, 2012

Copy link to clipboard

Copied

@Phill, thank you for your reply... nice tip!

I will be glad to help as soon as i understand a bit more about Cold Fusion...

Regards.

Votes

Translate

Translate

Report

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 ,
Apr 14, 2012 Apr 14, 2012

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

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
Community Expert ,
Apr 14, 2012 Apr 14, 2012

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

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 ,
Apr 14, 2012 Apr 14, 2012

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

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
Apr 16, 2012 Apr 16, 2012

Copy link to clipboard

Copied

LATEST

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.

Votes

Translate

Translate

Report

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
Documentation