home *** CD-ROM | disk | FTP | other *** search
- program testfield;
-
- { FIELD function by Sue Widemark }
-
- Uses
- Crt;
-
- { this function minics the FIELD function of data BASIC }
- { of the PICK Operating system and can be inserted }
- { into your I/O or other standard UNIT. It will extract a substring }
- { between delimiters. This is handy for DATES <7/14/89> say if you want to }
- { extract the year, you invoke field this way: YEAR := field(date,'/',3) }
- { and it will extract the '89'. You can also use it for spaces... For }
- { example, on the sentence, "the cow jumped over the moon", if you type this }
- { sentence at the prompt this program will give you (for testing purposes, }
- { this is a program), and type the space bar when it asks for delimiter, }
- { then when you put in a 4 for occurance i.e. for the fourth word of the }
- { sentence, it will give you correctly, the word, 'over'. If you have }
- { a bunch of items in one long string, it becomes very easy to extract }
- { one of them with the FIELD function i.e. JAN*FEB*MAR*APR*MAY*JUN }
- { to get MAY or the 5th month: 5th_month := field(datestring,'*',5 }
- { You will soon find this function making your programming life nice and }
- { easy without tedious substring extractions for this type of thing..enjoy!}
-
-
- VAR
-
- bx,lx : string;
- done : boolean;
- choice : char;
- chopped_string : string;
- occur : integer;
-
- function field(the_string : string;what : char;occ : integer): string;
-
- VAR
-
- place : integer;
- hold : string;
- i : integer;
- workstring,rest_of_string : string;
- len : integer;
-
- BEGIN
- workstring := the_string;
- i := 0;
- repeat
- i:= succ(i);
- place := pos(what,workstring);
- len := length(workstring);
- if place = 0 then hold := workstring else
- BEGIN
- hold := copy(workstring,1,place-1);
- rest_of_string := copy(workstring,place+1,len-place);
- workstring := rest_of_string;
- if i+1>occ then place:=0
- END;
- until (i> occ) or (place= 0);
- field := hold;
-
- END;
-
- { usage example }
-
- BEGIN
- done := false;
- repeat
- writeln;
- write('Enter string to extract values from <ex: 12/25/89>: '); readln(bx);
- write('Which char is the delimiter? ');readln(choice);
- write('Which part do you want to extract? '); readln(occur);
- chopped_string := field(bx,choice,occur);
- writeln('This is the result of the field function: ',chopped_string);
- writeln;write('Press "M" for more or "Q" to quit: ');
- choice := readkey;
- if UpCase(choice) <> 'M' then done := true;
- writeln;
- Until done;
- END.
- .. ...-....