1 | PXRRUTIL ;ISL/PKR - Utility routines for use by PXRR. ;3/20/97
|
---|
2 | ;;1.0;PCE PATIENT CARE ENCOUNTER;**12**;Aug 12, 1996
|
---|
3 | ;
|
---|
4 | ;=======================================================================
|
---|
5 | SORT(N,ARRAY,KEY) ;Sort an ARRAY with N elements into ascending order,
|
---|
6 | ;return the number of unique elements. KEY is the piece of ARRAY on
|
---|
7 | ;which to base the sort. The default is the first piece.
|
---|
8 | ;
|
---|
9 | I (N'>0)!(N=1) Q N
|
---|
10 | N IC,IND
|
---|
11 | I '$D(KEY) S KEY=1
|
---|
12 | F IC=1:1:N S ^TMP($J,"SORT",$P(@ARRAY@(IC),U,KEY))=@ARRAY@(IC)
|
---|
13 | S IND=""
|
---|
14 | F IC=1:1 S IND=$O(^TMP($J,"SORT",IND)) Q:IND="" D
|
---|
15 | . S @ARRAY@(IC)=^TMP($J,"SORT",IND)
|
---|
16 | K ^TMP($J,"SORT")
|
---|
17 | Q IC-1
|
---|
18 | ;
|
---|
19 | ;=======================================================================
|
---|
20 | STRREP(STRING,TS,RS) ;Replace every occurence of the target string (TS)
|
---|
21 | ;in STRING with the replacement string (RS).
|
---|
22 | ;Example 9.19 (page 220) in "The Complete Mumps" by John Lewkowicz:
|
---|
23 | ; F Q:STRING'[TS S STRING=$P(STRING,TS)_RS_$P(STRING,TS,2,999)
|
---|
24 | ;fails if any portion of the target string is contained in the with
|
---|
25 | ;string. Therefore a more elaborate version is required.
|
---|
26 | ;
|
---|
27 | N FROM,NPCS,STR
|
---|
28 | ;
|
---|
29 | I STRING'[TS Q STRING
|
---|
30 | ;Count the number of pieces using the target string as the delimiter.
|
---|
31 | S FROM=1
|
---|
32 | F NPCS=1:1 S FROM=$F(STRING,TS,FROM) Q:FROM=0
|
---|
33 | ;Extract the pieces and concatenate RS
|
---|
34 | S STR=""
|
---|
35 | F FROM=1:1:NPCS-1 S STR=STR_$P(STRING,TS,FROM)_RS
|
---|
36 | S STR=STR_$P(STRING,TS,NPCS)
|
---|
37 | Q STR
|
---|
38 | ;
|
---|