Link to home
Start Free TrialLog in
Avatar of Zac123
Zac123Flag for United Kingdom of Great Britain and Northern Ireland

asked on

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
Avatar of _agx_
_agx_
Flag of United States of America image

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>
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
Avatar of Zac123

ASKER

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! :-)
ASKER CERTIFIED SOLUTION
Avatar of _agx_
_agx_
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Zac123

ASKER

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
Welcome :)