Salesforce

4GL/ABL: How to get the frequency of a string in a list?

Information

 
Title4GL/ABL: How to get the frequency of a string in a list?
URL NameP20669
Article Number000171177
EnvironmentAll Supported Operating Systems
Progress 9.x
OpenEdge 10.x
Question/Problem Description
Steps to Reproduce
Clarifying Information
Error Message
Defect Number
Enhancement Number
Cause
Resolution

The following code demonstrates how to write a function that returns the number times a string occurs in a given list:

FUNCTION getFrequency RETURNS INTEGER
    (INPUT pcString AS CHARACTER,
     INPUT pcList AS CHARACTER,
     INPUT plCaseSensitive AS LOGICAL
     ):

/*------------------------------------------------------------------------------
Purpose: Returns the number of times a string occurs in a list or 0 if the string is not in the list.
Params : pcString: The string whose number of Frequencys (number of times it occurs) to return.
pcList : The delimited list to scan for number of the string Frequencys.
plCaseSensitive : The earch for pcString will be case sensitive if TRUE.
Syntax : getFrequency(pcString, pcList, plCaseSensitive).
------------------------------------------------------------------------------*/
IF LENGTH(pcString) > 1 THEN
    ASSIGN
        pcString = REPLACE (pcString , pcString ,CHR(2) )
        pcList = REPLACE ( pcList , pcString ,CHR(2) ).

IF NOT plCaseSensitive THEN
    ASSIGN
        pcString = CAPS(pcString)
        pcList = CAPS(pcList).

RETURN NUM-ENTRIES(pcList, pcString) - 1.
END FUNCTION.

/* Sample code to call the getFrequency function */
DEFINE VARIABLE cList AS CHARACTER NO-UNDO.
DEFINE VARIABLE cString AS CHARACTER NO-UNDO.
DEFINE VARIABLE lCaseSensitive AS LOGICAL NO-UNDO.
DEFINE VARIABLE iCounter AS INTEGER NO-UNDO.

/* initialize the string to a lower case letter */
ASSIGN
    cString = LC(CHR(RANDOM(65,90))).

/* Randomly populate the list with 26 letters */
ASSIGN
    cList = CHR(RANDOM(65,90)).
DO iCounter = 2 TO 26:
    cList = cList + "," + CHR(RANDOM(65,90)).
END.

/* Message the information obtained */
MESSAGE
    "The List:" "~t" cList "~n"
    "The String:" "~t"cString "~n"
    "Case Sensitive:" "~t" getFrequency(cString, cList, TRUE) "~n"
    "Not Sensitive:" "~t"getFrequency(cString, cList, FALSE)
VIEW-AS ALERT-BOX INFO BUTTONS OK.

Workaround
Notes
Keyword Phrase
Last Modified Date9/13/2015 5:26 PM

Powered by