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

Dynamically Named Session Variable?

Explorer ,
Dec 16, 2009 Dec 16, 2009

Copy link to clipboard

Copied

Is there any way that i can assign a session variable a name from a variable?

Something like this but that actually works?

<cfset variables.userid = 238>
<cfset session.user#variables.userid# = 1>

Thanks,

Ben

TOPICS
Advanced techniques

Views

604

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

Valorous Hero , Dec 16, 2009 Dec 16, 2009
<cfset session["user" & variables.userid] = 1>

Learn about array notation of ColdFusion variables, it is a powerful concept.

Votes

Translate

Translate
Valorous Hero ,
Dec 16, 2009 Dec 16, 2009

Copy link to clipboard

Copied

<cfset session["user" & variables.userid] = 1>

Learn about array notation of ColdFusion variables, it is a powerful concept.

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 ,
Dec 16, 2009 Dec 16, 2009

Copy link to clipboard

Copied

<cfset session["user" & variables.userid] = 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
Engaged ,
Dec 16, 2009 Dec 16, 2009

Copy link to clipboard

Copied

To elaborate on Ian's comment ...

There are two main forms of "structured variables" provided by ColdFusion.  (They are, of course, the "usual suspects" ...)

  • Arrays are simply one-based ordered collections of values.  (Each element can be a scalar, a struct, or an array.)
  • Structs are what Perl would call a hash, and other languages might call an "associative array."  Either way, elements are referred-to by keys which may be strings or numbers.  There are two equivalent notations:  variable.key and variable[key].  The practical difference between the two is that, in the second form, "key" may be an arbitrary expression.  The result of evaluating that expression becomes the key that is used.

The underlying implementation in both cases is provided by Java, because ColdFusion performs "on-the-fly" compilation into Java, which is what actually gets executed by the engine.

All of the predefined system variables are either arrays or structs, or at least, functionally appear to be.

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
Explorer ,
Dec 16, 2009 Dec 16, 2009

Copy link to clipboard

Copied

LATEST

Thank you very much!

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