Jump to content
View in the app

A better way to browse. Learn more.

FMForums.com

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Count number of times each letter is used in a phrase

Featured Replies

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

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 by comment

  • 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?"

 

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

Important Information

By using this site, you agree to our Terms of Use.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.