November 28, 20223 yr Happy holiday season, everybody. We have an ordering system for die-cut letters and I'd like to be able to parse out how many of each letter is used in a phrase into two separate fields (CharacterUsed and Count) so we can bill for the quantity of letter used, e.g., If someone types into the phrase field, Happy Birthday, Fred, the result of the script/calculation would be: a 2 B 1 d 2 ... y 2 ...where each record would be created in another table for each character used (and the quantity of each letter used.) I know the slow way would be to use a looping script using PatternCount for each character, but I'm wondering if there's a more efficient way than building a 60+ line script (letters, upper and lower case, numbers, and punctuation.) Cheers, Rich
November 28, 20223 yr First of all, PatternCount is not case-sensitive, so if you want separate items for B…2 and b…1 in "Happy Birthday, Bob!" you will need to use another method. The other thing is that a 60+ line script would not be "looping". A looping script would have exactly one block of instructions of what do with an individual letter, and the script would perform these instructions - you guessed it - in a loop for each letter in a given list. If you have a list of allowable letters, then you could simply loop over that (or preferably, over a list of allowable letters filtered by the phrase). Alternatively, you could loop over the letters in the given phrase - for example: Set Variable [ $parentID; Value:Parent::ParentID ] Set Variable [ $phrase; Value:Parent::Phrase ] Go to Layout [ “Child” (Child) ] Loop Exit Loop If [ IsEmpty ( $phrase ) ] Set Variable [ $char; Value:Left ( $phrase ; 1 ) ] New Record/Request Set Field [ Child::ParentID; $parentID ] Set Field [ Child::Letter; $char ] Set Field [ Child::Quantity; Length ( Filter ( $phrase ; $char ) ) ] Set Variable [ $phrase; Value:Substitute ( $phrase ; $char ; "" ) ] End Loop Go to Layout [ original layout ] and that's just 13 lines. Edited November 28, 20223 yr by comment
November 28, 20223 yr Author As always, spot on. Many thanks. You freaked me out for a few minutes because I coded what you posted originally early this morning, then came back to check what I had coded against here a few minutes ago (to troubleshoot a script failure--my fault), and the coding had changed here. I looked askance at my coffee cup here at work and thought, "Okay, who put something in my coffee?"
November 28, 20223 yr LOL, there's an "Edited" tag at the bottom of my post. The previous version worked just as well, but it was 16 lines and - more importantly - less efficient.
Create an account or sign in to comment