search for chars

BelaB

New Member
How can I search for characters in a db-field?
I want to count up them, eg. "How many "a"s are in this field!

Thank you!
 

KubiaK

Member
Try this :

DEF VAR i AS INT INIT 0.
DEF VAR db-field AS CHAR INIT "This is a test". /* Your db-field */
DEF VAR X AS CHAR INIT "i". /* Searched letter */
DEF VAR RESULT AS INT.

REPEAT :
ASSIGN i = i + 1.
IF SUBSTRING(db-field , i , 1) = X THEN ASSIGN RESULT = RESULT + 1.
IF i = LENGTH (db-field) THEN LEAVE.

END.

DISP RESULT.
 

Crittar

Member
define variable c-list as character format 'x' extent 50 no-undo.
define vatriable c-count as integer extent 50 no-undo.
define variable x as integer no-undo.
define variable y as integer no-undo.
define variable top-idx as integer no-undo.
define variable got-one as logical no-undo.

do x = 1 to length(dbfield):
if top-idx = 0 then
assign
y = 1
top-idx = 1
c-list[1] = substring(dbfield,x,1)
c-count[1] = 1
got-one = true.
else
do y = 1 to top-idx:
if c-list[y] = substring(dbfield,x,1) then
assign
c-count[y] = c-count[y] + 1
got-one = true.
end. /* end do y = 1 to top-idx */

if not got-one then
assign
top-idx = top-idx + 1
c-list[top-idx] = substring(dbfield,x,1)
c-count[top-idx] = 1.

assign
got-one = false.

end. /* end do x = 1 to length(dbfield) */

do x = 1 to top-idx:
display
c-list[x]
c-count[x]
with frame fr1
no-labels
down.

down with frame fr1.

end. /* end do x = 1 to top-idx */

Hope this is also useful for you.
 

Crittar

Member
I should have pointed out:

The above code returns a count of the number of each character which occurs in dbfield without you having to specify the character you are looking for.

:clown:
 
Top