Jump to content
Server Maintenance This Week. ×

Count number of times each letter is used in a phrase


This topic is 513 days old. Please don't post here. Open a new topic instead.

Recommended Posts

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

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

This topic is 513 days old. Please don't post here. Open a new topic instead.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

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