source: trunk/STRING/strtrans.pro @ 7

Last change on this file since 7 was 7, checked in by pinsard, 18 years ago

1st commit according to cerbere.lodyc.jussieu.fr:/usr/home/smasson/IDL_RD/

  • Property svn:eol-style set to native
  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 4.6 KB
Line 
1;
2;+
3; NAME:
4;       STRTRANS
5; PURPOSE:
6;       Translate all occurences of one substring to another.
7; CATEGORY:
8;       text/strings
9; CALLING SEQUENCE:
10;       new = strtrans(oldstr,from,to,ned)
11; INPUTS:
12;       oldstr -- string on which to operate.              in
13;                 May be an array.
14;       from   -- substrings to be translated. May be      in
15;                 an array.
16;       to     -- what strings in from should be           in
17;                 translated to. May be an array.
18; KEYWORD PARAMETERS:
19;       /HELP  -- Set this to print useful message and
20;                 exit.
21; OUTPUTS:
22;       new    -- Translated string. Array if oldstr is    out         
23;                 an array.
24;       ned    -- number of substitutions performed in     out
25;                 oldstr.  Array if oldstr is an array.
26; COMMON BLOCKS:
27; SIDE EFFECTS:
28; NOTES:
29;       - Any of old, from, and to can be arrays. 
30;       - from and to must have the same number of elements.
31; EXAMPLE:
32;       inp='Many*bad!chars+in_here'
33;       from=['*','!','+','_']
34;       to  =[' ',' ',' ',' ']
35;       out = strtrans(inp,from,to,ned)
36;       Will produce out='Many bad chars in here', and set ned to 4.
37; MODIFICATION HISTORY:
38;       $Id$
39;       $Log: strtrans.pro,v $
40;       Revision 1.3  1996/06/14 20:00:27  mcraig
41;       Updated Copyright info.
42;
43;       Revision 1.2  1996/05/09 00:22:17  mcraig
44;       Sped up significantly by using str_sep to handle the translation.  No longer
45;       relies on routines fromother user libraries.
46;
47;       Revision 1.1  1996/01/31 18:47:37  mcraig
48;       Initial revision
49;
50; RELEASE:
51;       $Name: Rel_1_2 $
52;
53; COPYRIGHT:
54;  Copyright (C) 1996 The Regents of the University of California, All
55;  Rights Reserved.  Written by Matthew W. Craig.
56;  See the file COPYRIGHT for restrictions on distrubting this code.
57;  This code comes with absolutely NO warranty; see DISCLAIMER for details.
58;-
59;
60FUNCTION strtrans, InputString, from, to, ned,  $
61                   HELP=Help
62
63; Bomb out to caller if error.
64    On_error, 2
65
66; Offer help if we don't have at least InputString, from, and to, or
67; if the user asks for it.
68    IF (n_params() LT 3) OR keyword_set(help) THEN BEGIN
69        offset = '   '
70        print, offset+'Translate all occurences of one substring to another.'
71        print, offset+'new = strtrans(oldstr,from,to,ned)'
72        print, offset+'Inputs:'
73        print, offset+offset+'oldstr -- string on which to operate.              in'
74        print, offset+offset+'          May be an array.'
75        print, offset+offset+'from   -- substrings to be translated. May be      in'
76        print, offset+offset+'          an array.'
77        print, offset+offset+'to     -- what strings in from should be           in'
78        print, offset+offset+'          translated to. May be an array.'
79        print, offset+'Outputs:'
80        print, offset+offset+'new    -- Translated string. Array if oldstr is    out'
81        print, offset+offset+'          an array.'
82        print, offset+offset+'ned    -- number of substitutions performed in     out'
83        print, offset+offset+'          oldstr.  Array if oldstr is an array.'
84        print, offset+'Notes:'
85        print, offset+offset+'- Any of old, from, and to can be arrays. '
86        print, offset+offset+'- from and to must have the same number of elements.'
87        return, -1
88    ENDIF
89   
90    strn = InputString
91
92;  Check that From/To have same number of elements.  RETURN if they don't.
93    NFrom = n_elements(from)
94    NTo = n_elements(to)
95    IF (NFrom EQ 0) OR (NTo EQ 0) THEN return, strn
96    IF NFrom NE NTo THEN BEGIN
97        print,'Error: Number of elements in from/to unequal'
98        return,-1
99    ENDIF
100
101;  Make sure there are no null strings in From.  RETURN if there are.   
102    FromLen = strlen(From)
103    IF (total(FromLen EQ 0) GT 0) THEN BEGIN
104        print, 'Error: elements of From must have nonzero length.'
105        return, -1
106    ENDIF
107
108    NStrings = n_elements(strn)
109    ned = lonarr(NStrings)
110    tmpned = 0L
111
112; Say strn='a#b#c', from='#' and to='@'.  Then the approach here is to
113; first split strn at all occurances of '#', then recombine the pieces
114; with '@' inserted instead.  Do this for all elements of strn, and
115; all elements of from.
116    FOR i = 0L, NStrings-1 DO BEGIN
117        ned(i) = 0L
118        FOR j=0L, NFrom-1 DO BEGIN
119            SepStr = str_sep(strn(i), from(j))
120            NSubs = n_elements(SepStr) - 1
121            strn(i) = SepStr(0)
122            FOR k=1L, NSubs DO strn(i) = strn(i) + To(j) + SepStr(k)
123            ned(i) =  ned(i) + NSubs
124        ENDFOR
125    ENDFOR
126
127    return, strn
128END
Note: See TracBrowser for help on using the repository browser.