Consultor Eletrônico



Kbase P20669: 4GL/ABL: How to get the frequency of a string in a list?
Autor   Progress Software Corporation - Progress
Acesso   Público
Publicação   03/11/2008
Status: Verified

GOAL:

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

GOAL:

How to write a user defined function to return the number a string occurs in a list?

FACT(s) (Environment):

All Supported Operating Systems
Progress 9.x
OpenEdge 10.x

FIX:

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.