House of Fusion
Search over 2,500 ColdFusion resources here
  
Home of the ColdFusion Community

Mailing Lists
Home /  Groups /  ColdFusion Talk (CF-Talk)

Evaluating Strings

  << Previous Post |  RSS |  Sort Oldest First |  Sort Latest First |  Subscribe to this Group Next >> 
Top  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Jennifer Schlender
05/21/2004 04:41 PM

I have 4 text fields all named serialnum. When the user enters their serial numbers, on the next page I make sure that the numbers match to a corresponding regular expression. If one does not match, then the user is notified that they entered a wrong serial number and should enter it again. If they all pass the regex, they get inserted into a table. Well, I need to either evaluate those numbers before I insert or pull them back out and make sure that none of them are within 5 digits of each other. Example list: 3M0123456789, 2KB234234345, 22L897657898, 3M0123456790. All of these serial numbers are contained on one invoice. I would need to flag this invoice as the first number in the list is 1 digit lower than the last number and the first 3 characters match. I have attempted to do my insert first, and then compare the group by selected them again and doing a loop, but I can't get that to work. Any ideas? Thanks!

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Barney Boisvert
05/21/2004 04:52 PM

After the strings all pass the RE, do something like this (shot from the hip, with no testing): <cfset s = structNew() /> <cfloop list="#form.serialnum#" index="num">   <cfset prefix = left(num, 3) />   <cfset num = removeChars(num, 1, 3) />   <cfif structKeyExists(s, prefix)>     <cfset s[prefix] = listAppend(s[prefix], num) />   <cfelse>     <cfset s[prefix] = num />   </cfif> </cfloop> <cfloop collection="#s#" item="prefix">   <cfif listLen(s[prefix]) GT 1>     <cfset s[prefix] = listSort(s[prefix], "numeric") />     <cfset last = -10 />     <cfloop list="#s[prefix]#" index="num">       <cfif num - last LTE 5>         <cfthrow type="InvalidSerialException"           message="the #prefix##num# serial is invalid" />       </cfif>       <cfset last = num />     </cfloop>   </cfif> </cfloop> <cfoutput>the serials are all valid</cfoutput> Cheers, barneyb ----- Excess quoted text cut - see Original Post for more -----

Top  |   Parent  |   Reply  |   Original Post  |   RSS Feed  |   Subscribe to this Group
Author:
Jennifer Schlender
05/21/2004 05:23 PM

That worked PERFECT! I have struggled with this for a while and no one has been able to help :-( Thanks! ----- Excess quoted text cut - see Original Post for more -----


<< Previous Thread Today's Threads Next Thread >>

Search cf-talk

February 09, 2012

<<   <   Today   >   >>
Su Mo Tu We Th Fr Sa
       1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29