Jeepers Creepers
Jeepers Creepers

Reputation: 115

Check values in a list are all identical

Ok here's a thing, I have a form which when submitted can only update a table when a particular checkbox form variable values are identical, if one is different then it should proceed to update the table. So basically the user is selecting different rows and hitting the submit button.

So for example a good submission would be

form.claimid = 12,12,12,12,12

a bad submission would be

 form.claimid = 1,5,77,33,4,

I'm not sure how to check in a list if all the values in the form.claimid list are identical or not?

I would appreciate any ideas on this.

Upvotes: 3

Views: 663

Answers (2)

David Faber
David Faber

Reputation: 12485

If you're stuck on CF 9 or lower, you could loop over the list and put the elements into a struct:

<cfset list_coll = structNew() />
<cfloop list="#mylist#" index="myitem">
    <cfset list_coll[myitem] = "dummy" />
</cfloop>
<cfif structCount(list_coll) EQ 1>
    <!--- This is good --->
<cfelse>
    <!--- This is bad --->
</cfif>

The reason this works is that structs can't have duplicate keys.

Upvotes: 0

Regular Jo
Regular Jo

Reputation: 5510

You can also use CF's native ListRemoveDuplicates() if the version is sufficient (CF10, Railo/Lucee 4)

https://wikidocs.adobe.com/wiki/display/coldfusionen/ListRemoveDuplicates

<cfif ListLen(ListRemoveDuplicates(mylist)) eq 1>

I'll leave this as an alternate means for older CFs.

I'm not sure how to check in a list if all the values in the form.claimid list are identical or not?

There are a couple ways, most of which involve looping, but I'm going to show you a regular expression that can do this. We're going to get the first value using ListFirst, and use a ReReplace on that value to see if every other value matches.

I use a loop here but only as a demonstration.

<cfoutput>
<cfset mylist = "11,22,33,44|44,44,33,44|557,557,557">
<cfloop list="#mylist#" index="m" delimiters="|">
  <cfset matchele = listfirst(m)>
  <cfset eradicate = rereplace(m,"(?:(?:^|,)#matchele#(?=,|$))+","","ALL")>
  "#m#", "#matchele#", "#eradicate#"<br />
</cfloop>
</cfoutput>

Upvotes: 2

Related Questions