Skip to main content
2Charlie
Inspiring
February 29, 2016
Question

What is the best way to read value from a session variable that has comma separated values?

  • February 29, 2016
  • 2 replies
  • 1053 views

I'm comparing values in a session variable that are in comma separated to local JSON values. What is the best way to read the comma separated value from the session.groups variable?

    This topic has been closed for replies.

    2 replies

    BKBK
    Community Expert
    Community Expert
    March 5, 2016

    2Charlie wrote:

    What is the best way to read the comma separated value from the session.groups variable?

    The best way in Coldfusion to read a variable that consists of comma-separated values is to treat it as a list.

    Carl Von Stetten
    Legend
    March 1, 2016

    Show us some sample data and what you mean by "comparing".

    2Charlie
    2CharlieAuthor
    Inspiring
    March 1, 2016

    The session variable contains values like this:

    KB.DEVUNITS.BUSINESS-COMMUNICATION,KB.DEVUNITS.ACCOUTING-FINANCE,KB.DEVUNITS.BIOLOGY,KB.FILES.ADMIN,KB.DEVUNITS.NURSING,KB.DEVUNITS.ARMY-ROTC,KB.DEVUNITS.MANAGEMENT-MARKETING,KB.DEVUNITS.ADMISSIONS,KB.DEVTRAINING,KB.DEVUNITS.HOUSING-RESIDENCE-LIFE,KB.DEVUNITS.ARTS-AND-SCIENCE,KB.DEVUNITS.ON-CAMPKB-MBA,KB.DEVUNITS.ATHLETICS,KB.DEVUNITS.INFORMATION-SYSTEMS,KB.DEVUNITS.COLLEGE-OF-BUSINESS,KB.DEVUNITS.STUDENT-SENATE,KB.DEVUNITS.CAMPKB-LIFE,KB.DEVUNITS.POWER-OF-AND,KB.DEVUNITS.CHAASE,KB.DEVDEVELOPER,KB.DEVUNITS.NEWS,KB.CSADMIN,KB.DEVUNITS.CADE,KB.DEVUNITS.CONTINUING-ED,KB.DEVUNITS.COLLEGE-OF-EDUCATION-AND-HUMAN-SCIENCES,KB.DEVAUTHOR.ACCESS,KB.DEVUNITS.KOKB,KB.DEVUNITS.SALES-CENTER,KB.DEVUNITS.LANGUAGES,KB.CSDEVELOPER,KB.DEVUNITS.NURSING-HEALTH-SCIENCES,KB.KBREDESIGN,KB.DEVUNITS.SPECIAL-EDUCATION,KB.DEVUNITS.ON-CAMPKB-DINING,KB.DEVUNITS.RECREATION-INTRAMURALS,KB.DEVUNITS.OFFICE-OF-INTERDISCIPLINARY-PROGRAMS,KB.DEVUNITS.PSYCH,KB.DEVADMIN,KB.DEVUNITS.ACADEMICS

    In the loop of each of the JSON data, I want to compare if the data in the json matches one or more of the data in the session variable.

    Carl Von Stetten
    Legend
    March 1, 2016

    How about a sample of the JSON data?

    In general, comma-separated data stored in ColdFusion variables are treated as lists, so you can use any of the LISTxxx() functions on them.  In your case, you can probably use ListFind() or ListFindNoCase() (the latter if the JSON data isn't in upper case like the sample data you showed above.  Also, you can turn a list into an array using ListToArray(), then use Arrayxxx() functions to match your values.  Arrays usually perform a bit better than lists if the lists are fairly long.