Stepping thru a field.

mndrich

New Member
How can i step thru a field that is 500 characters long? It will contain many different characters and when i find the one i am searching for, i want to store the position of that character in a variable.
 

mndrich

New Member
New info

Thanks for replying. I looked at that function, the problem i am having and i should have said it in the post, i need to continue searching even after i find the character i am searching for and continue to store the positions of each occurence of that character.


Crittar said:
required-position = INDEX("string to search","string to search for").
 

erich

New Member
Try this, instead of using an array as I did, you could use a comma delimite string (or whatever) to store the positions. If you use an array, make sure to set the extent high enough to handle the maximum possible entries found.

DEF VAR V-FOUND-AT-POSITIONS AS INT EXTENT 50 NO-UNDO.
DEF VAR V-CURRENT-POSITION AS INT NO-UNDO.
DEF VAR V-STARTING-POSITION AS INT NO-UNDO INIT 1. /*MUST SET THIS!*/
DEF VAR V-STRING-TO-SEARCH AS CHAR INIT "ABCDEFABCDEFABCDEFABCDEFABCDEFABCDEFABCDEF" NO-UNDO.
DEF VAR V-COUNTER AS INT NO-UNDO.
DEF VAR V-STRING-TO-FIND AS CHAR NO-UNDO.

ASSIGN V-STRING-TO-FIND = "CD".
DO V-COUNTER = 1 TO NUM-ENTRIES(V-STRING-TO-SEARCH, V-STRING-TO-FIND) - 1:
ASSIGN V-CURRENT-POSITION = INDEX(V-STRING-TO-SEARCH, V-STRING-TO-FIND, V-STARTING-POSITION).
ASSIGN V-STARTING-POSITION = V-CURRENT-POSITION + 1.
ASSIGN V-FOUND-AT-POSITIONS[V-COUNTER] = V-CURRENT-POSITION.
END.

After running, my array held (3, 9, 15, 21, 27, 33, 39) which is what I believe you want, correct? :)
 

mndrich

New Member
Thanks

Thanks for the reply, this looks like it is what i am looking for. I will give this a a try...thanks again...



erich said:
Try this, instead of using an array as I did, you could use a comma delimite string (or whatever) to store the positions. If you use an array, make sure to set the extent high enough to handle the maximum possible entries found.

DEF VAR V-FOUND-AT-POSITIONS AS INT EXTENT 50 NO-UNDO.
DEF VAR V-CURRENT-POSITION AS INT NO-UNDO.
DEF VAR V-STARTING-POSITION AS INT NO-UNDO INIT 1. /*MUST SET THIS!*/
DEF VAR V-STRING-TO-SEARCH AS CHAR INIT "ABCDEFABCDEFABCDEFABCDEFABCDEFABCDEFABCDEF" NO-UNDO.
DEF VAR V-COUNTER AS INT NO-UNDO.
DEF VAR V-STRING-TO-FIND AS CHAR NO-UNDO.

ASSIGN V-STRING-TO-FIND = "CD".
DO V-COUNTER = 1 TO NUM-ENTRIES(V-STRING-TO-SEARCH, V-STRING-TO-FIND) - 1:
ASSIGN V-CURRENT-POSITION = INDEX(V-STRING-TO-SEARCH, V-STRING-TO-FIND, V-STARTING-POSITION).
ASSIGN V-STARTING-POSITION = V-CURRENT-POSITION + 1.
ASSIGN V-FOUND-AT-POSITIONS[V-COUNTER] = V-CURRENT-POSITION.
END.

After running, my array held (3, 9, 15, 21, 27, 33, 39) which is what I believe you want, correct? :)
 

Crittar

Member
Code:
DEFINE VARIABLE posn AS INTEGER EXTENT 10 NO-UNDO. /* set the extent to the maximum number of likely positions */
DEFINE VARIABLE idx AS INTEGER NO-UNDO.
DEFINE VARIABLE tmp-pos AS INTEGER NO-UNDO.
DEFINE VARIABLE last-pos AS INTEGER NO-UNDO.
 
ASSIGN
tmp-pos = 1
idx = 0
last-pos = 1.
 
DO WHILE last-pos <> 0:
 
ASSIGN
	 idx = idx + 1
	 posn[idx] = INDEX("string to search","string to search for",tmp-pos).
 
IF idx <> 0 THEN
	ASSIGN
	 tmp-pos = posn[idx] + 1.
 
ASSIGN
	 last-pos = posn[idx].
END.

I've not tested this (typed it in on the fly) but I think it should work, at worst it should only need minor mods.


Looks like Erich beat me to the punch but I'll leave my suggestion here anyway.
 

erich

New Member
I'm thinking the 2nd solution might work better...I assumed that progress allowed more than 1 character for a delimiter (as used in my num-entries function) and I'm not sure that you'll get the proper number of entries (iterations) if the text you're searching for is more than one character...Guess it depends on exactly what you need to look for. Give solution #2 a try though, looks good.
 
Top