home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / PASCAL / FIELD.ZIP / FIELD.PAS
Encoding:
Pascal/Delphi Source File  |  1989-09-05  |  2.5 KB  |  80 lines

  1. program testfield;
  2.  
  3. { FIELD function by Sue Widemark }
  4.  
  5. Uses
  6.   Crt;
  7.  
  8. { this function minics the FIELD function of data BASIC  }
  9. { of the PICK Operating system and can be inserted }
  10. { into your I/O  or other standard UNIT.  It will extract a substring  }
  11. { between delimiters.  This is handy for DATES <7/14/89> say if you want to }
  12. { extract the year, you invoke field this way:  YEAR := field(date,'/',3)  }
  13. { and it will extract the '89'.  You can also use it for spaces...  For }
  14. { example, on the sentence, "the cow jumped over the moon", if you type this }
  15. { sentence at the prompt this program will give you (for testing purposes, }
  16. { this is a program), and type the space bar when it asks for delimiter,  }
  17. { then when you put in a 4 for occurance i.e. for the fourth word of the }
  18. { sentence, it will give you correctly, the word, 'over'. If you have }
  19. { a bunch of items in one long string, it becomes very easy to extract }
  20. { one of them with the FIELD function i.e. JAN*FEB*MAR*APR*MAY*JUN }
  21. { to get MAY or the 5th month:  5th_month := field(datestring,'*',5 }
  22. { You will soon find this function making your programming life nice and }
  23. { easy without tedious substring extractions for this type of thing..enjoy!}
  24.  
  25.  
  26. VAR
  27.  
  28.   bx,lx : string;
  29.   done     : boolean;
  30.   choice : char;
  31.   chopped_string : string;
  32.   occur : integer;
  33.  
  34. function field(the_string : string;what : char;occ : integer): string;
  35.  
  36. VAR
  37.  
  38.    place :  integer;
  39.    hold : string;
  40.    i : integer;
  41.    workstring,rest_of_string : string;
  42.    len : integer;
  43.  
  44. BEGIN
  45.  workstring := the_string;
  46.  i := 0;
  47.  repeat
  48.   i:= succ(i);
  49.   place := pos(what,workstring);
  50.   len := length(workstring);
  51.   if place = 0 then hold := workstring else
  52.    BEGIN
  53.     hold := copy(workstring,1,place-1);
  54.     rest_of_string := copy(workstring,place+1,len-place);
  55.     workstring := rest_of_string;
  56.     if i+1>occ then place:=0
  57.    END;
  58.  until (i> occ) or (place= 0);
  59.  field := hold;
  60.  
  61. END;
  62.  
  63. { usage example }
  64.  
  65. BEGIN
  66.   done := false;
  67.   repeat
  68.    writeln;
  69.    write('Enter string to extract values from <ex: 12/25/89>: '); readln(bx);
  70.    write('Which char is the delimiter? ');readln(choice);
  71.    write('Which part do you want to extract? '); readln(occur);
  72.    chopped_string := field(bx,choice,occur);
  73.    writeln('This is the result of the field function: ',chopped_string);
  74.    writeln;write('Press "M" for more or "Q" to quit: ');
  75.    choice := readkey;
  76.    if UpCase(choice) <> 'M' then done := true;
  77.    writeln;
  78.   Until done;
  79. END.
  80. .. ...-....