Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Retrieve character positons from string

Status
Not open for further replies.

Prisk

Programmer
Oct 8, 2007
9
GB
Just wondering if anyone out there can help me with this:-

I'm searching a large string for all instances of character 'I' and need to establish not only the number of instances of 'I' but also their position within the string (ie - 1st/2nd etc).
I know I can use:-

numberVar z := InStr({field to be searched},"I")

but this will only set the variable z to the position of the first instance of 'I', whereas I need to get the position of each instance (to later be used in another formula).

Any ideas?
 
use an array to store positions

global numbervar array posI:= [0,0,0,0,0,0,0,0,0,0,];
global numbervar countI:=0;
global numbervar strpos:= 1;


while strpos <> length({stringField}) do
( If {stringField}[strpos] = 'I'
then
countI:= countI+1;
posI[countI]:= strpos;
strpos:= strpos+1;
);

YOu can then use the variables in other formulae to show CountI, and test array positions to show where I's occurred.

Or you can use Join (global numbervar array posI, ",")
to list positions separated by ,

Ian




 
Thanks Ian, the only problem I have is that the string I am searching is 780 characters long! Is this the only way?
 
Sorry can't think of another way. Perhaps someone else can come up with a better idea.

Ian
 
Try posting what the formula used by "to later be used in another formula" is.

-k
 
The formula will be just a DateAdd using each positon of I (as I is an instance of sickness on a register), to work out the date of each I(or 'illness').

I've setup the array as Ian described but am now getting
-
subscript must be between 1 and array size -
posI[countI]:= strpos;


Any ideas?
 
Perhaps your array is too small, I only set as 10 elements. Since your string can be up to 780, you may be finding more than 10 x I.

Set number of elements as to cover maximum number of I's you are likely to find.

In your dateadd formula you will need to handle 0, as any elements unused will remain as 0. Thus your formula must know to stop when the first zero is found.

Ian

Ian
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top