[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 557
  • Last Modified:

cfif, listLast, need to find a char at the end of a variable

i'm looping through a list and outputting variables like so:

<cfloop list="#ColList#" index="ColList">
<cfoutput>#ColList#</cfoutput>
</cfloop>

each variable which is outputted ends with either _1 or _2

what i need is this:

CFIF variable ends in _1
THEN do this
IF it ends in _2
THEN do this
otherwise just do this.


thats the plain english version. :-) can anyone helpo me do this?

thanks
0
bede123
Asked:
bede123
  • 4
  • 2
1 Solution
 
_agx_Commented:
First, make sure you use different variables name for  the "list" and "index". Then assuming the values always contain

<cfloop list="#ColList#" index="theItem">
    <cfset suffix = listLast(theItem, "_")>
    <cfif suffix eq "1">
         it's 1. do something
    <cfelseif suffix eq "2">
         it's 2. do something else
     <cfelse>
          not 1 or 2. do something else
     </cfif>
</cfloop>
0
 
_agx_Commented:
Oops, posted too soon.  I meant to say: assuming the values always contain a "_", then use the code above. Otherwise, you might want to add validation of the listLen
0
 
bede123Author Commented:
That's perfect thanks. I can ensure that _ does exist by validating a couple of steps prior to this one.

I've been trying all night to put this together! I didn't even have Suffix in my version! :-)
0
Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

 
_agx_Commented:
I chose to create a separate variable named "suffix" for readability.  But technically you could just use the listLast() function twice.

  <cfif listLast(theItem, "_") eq "1">
         it's 1. do something
    <cfelseif listLast(theItem, "_") eq "2">
         it's 2. do something else
     <cfelse>
          not 1 or 2. do something else
     </cfif>
0
 
bede123Author Commented:
oh i see, i thought.... oh never mind....                      so much to learn!

i decided to use the one without the suffix.

many thanks (again)

z
0
 
_agx_Commented:
Welcome :)
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now