annotate scripts/strings/index.m @ 2276:6dedd4e0a82f

[project @ 1996-05-24 04:58:21 by jwe]
author jwe
date Fri, 24 May 1996 04:58:21 +0000
parents 707b9396ad2c
children 5cffc4b8de57
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2276
6dedd4e0a82f [project @ 1996-05-24 04:58:21 by jwe]
jwe
parents: 2271
diff changeset
1 # Copyright (C) 1996 Kurt Hornik
2271
707b9396ad2c [project @ 1996-05-24 02:22:01 by jwe]
jwe
parents:
diff changeset
2 #
707b9396ad2c [project @ 1996-05-24 02:22:01 by jwe]
jwe
parents:
diff changeset
3 # This file is part of Octave.
707b9396ad2c [project @ 1996-05-24 02:22:01 by jwe]
jwe
parents:
diff changeset
4 #
707b9396ad2c [project @ 1996-05-24 02:22:01 by jwe]
jwe
parents:
diff changeset
5 # Octave is free software; you can redistribute it and/or modify it
707b9396ad2c [project @ 1996-05-24 02:22:01 by jwe]
jwe
parents:
diff changeset
6 # under the terms of the GNU General Public License as published by the
707b9396ad2c [project @ 1996-05-24 02:22:01 by jwe]
jwe
parents:
diff changeset
7 # Free Software Foundation; either version 2, or (at your option) any
707b9396ad2c [project @ 1996-05-24 02:22:01 by jwe]
jwe
parents:
diff changeset
8 # later version.
707b9396ad2c [project @ 1996-05-24 02:22:01 by jwe]
jwe
parents:
diff changeset
9 #
707b9396ad2c [project @ 1996-05-24 02:22:01 by jwe]
jwe
parents:
diff changeset
10 # Octave is distributed in the hope that it will be useful, but WITHOUT
707b9396ad2c [project @ 1996-05-24 02:22:01 by jwe]
jwe
parents:
diff changeset
11 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
707b9396ad2c [project @ 1996-05-24 02:22:01 by jwe]
jwe
parents:
diff changeset
12 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
707b9396ad2c [project @ 1996-05-24 02:22:01 by jwe]
jwe
parents:
diff changeset
13 # for more details.
707b9396ad2c [project @ 1996-05-24 02:22:01 by jwe]
jwe
parents:
diff changeset
14 #
707b9396ad2c [project @ 1996-05-24 02:22:01 by jwe]
jwe
parents:
diff changeset
15 # You should have received a copy of the GNU General Public License
707b9396ad2c [project @ 1996-05-24 02:22:01 by jwe]
jwe
parents:
diff changeset
16 # along with Octave; see the file COPYING. If not, write to the Free
707b9396ad2c [project @ 1996-05-24 02:22:01 by jwe]
jwe
parents:
diff changeset
17 # Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
707b9396ad2c [project @ 1996-05-24 02:22:01 by jwe]
jwe
parents:
diff changeset
18
707b9396ad2c [project @ 1996-05-24 02:22:01 by jwe]
jwe
parents:
diff changeset
19 function n = index (s, t)
707b9396ad2c [project @ 1996-05-24 02:22:01 by jwe]
jwe
parents:
diff changeset
20
707b9396ad2c [project @ 1996-05-24 02:22:01 by jwe]
jwe
parents:
diff changeset
21 # usage: index (s, t)
707b9396ad2c [project @ 1996-05-24 02:22:01 by jwe]
jwe
parents:
diff changeset
22 #
707b9396ad2c [project @ 1996-05-24 02:22:01 by jwe]
jwe
parents:
diff changeset
23 # Returns the position of the first occurence of the string T in the
707b9396ad2c [project @ 1996-05-24 02:22:01 by jwe]
jwe
parents:
diff changeset
24 # string S or 0 if no occurence is found.
707b9396ad2c [project @ 1996-05-24 02:22:01 by jwe]
jwe
parents:
diff changeset
25 #
707b9396ad2c [project @ 1996-05-24 02:22:01 by jwe]
jwe
parents:
diff changeset
26 # NOTE: this function does not work for arrays of strings.
707b9396ad2c [project @ 1996-05-24 02:22:01 by jwe]
jwe
parents:
diff changeset
27
707b9396ad2c [project @ 1996-05-24 02:22:01 by jwe]
jwe
parents:
diff changeset
28 # This is patterned after the AWK function of the same name.
707b9396ad2c [project @ 1996-05-24 02:22:01 by jwe]
jwe
parents:
diff changeset
29
707b9396ad2c [project @ 1996-05-24 02:22:01 by jwe]
jwe
parents:
diff changeset
30 # Original version by Kurt Hornik <Kurt.Hornik@ci.tuwien.ac.at>.
707b9396ad2c [project @ 1996-05-24 02:22:01 by jwe]
jwe
parents:
diff changeset
31
707b9396ad2c [project @ 1996-05-24 02:22:01 by jwe]
jwe
parents:
diff changeset
32 if (nargin != 2)
707b9396ad2c [project @ 1996-05-24 02:22:01 by jwe]
jwe
parents:
diff changeset
33 usage ("index (s, t)");
707b9396ad2c [project @ 1996-05-24 02:22:01 by jwe]
jwe
parents:
diff changeset
34 endif
707b9396ad2c [project @ 1996-05-24 02:22:01 by jwe]
jwe
parents:
diff changeset
35
707b9396ad2c [project @ 1996-05-24 02:22:01 by jwe]
jwe
parents:
diff changeset
36 n = 0;
707b9396ad2c [project @ 1996-05-24 02:22:01 by jwe]
jwe
parents:
diff changeset
37
707b9396ad2c [project @ 1996-05-24 02:22:01 by jwe]
jwe
parents:
diff changeset
38 if (isstr (s) && isstr (t))
707b9396ad2c [project @ 1996-05-24 02:22:01 by jwe]
jwe
parents:
diff changeset
39
707b9396ad2c [project @ 1996-05-24 02:22:01 by jwe]
jwe
parents:
diff changeset
40 l_s = length (s);
707b9396ad2c [project @ 1996-05-24 02:22:01 by jwe]
jwe
parents:
diff changeset
41 l_t = length (t);
707b9396ad2c [project @ 1996-05-24 02:22:01 by jwe]
jwe
parents:
diff changeset
42
707b9396ad2c [project @ 1996-05-24 02:22:01 by jwe]
jwe
parents:
diff changeset
43 if (l_t <= l_s)
707b9396ad2c [project @ 1996-05-24 02:22:01 by jwe]
jwe
parents:
diff changeset
44 tmp = l_s - l_t + 1;
707b9396ad2c [project @ 1996-05-24 02:22:01 by jwe]
jwe
parents:
diff changeset
45 for idx = 1 : tmp
707b9396ad2c [project @ 1996-05-24 02:22:01 by jwe]
jwe
parents:
diff changeset
46 if (strcmp (substr (s, idx, l_t), t))
707b9396ad2c [project @ 1996-05-24 02:22:01 by jwe]
jwe
parents:
diff changeset
47 n = idx;
707b9396ad2c [project @ 1996-05-24 02:22:01 by jwe]
jwe
parents:
diff changeset
48 return;
707b9396ad2c [project @ 1996-05-24 02:22:01 by jwe]
jwe
parents:
diff changeset
49 endif
707b9396ad2c [project @ 1996-05-24 02:22:01 by jwe]
jwe
parents:
diff changeset
50 endfor
707b9396ad2c [project @ 1996-05-24 02:22:01 by jwe]
jwe
parents:
diff changeset
51 endif
707b9396ad2c [project @ 1996-05-24 02:22:01 by jwe]
jwe
parents:
diff changeset
52
707b9396ad2c [project @ 1996-05-24 02:22:01 by jwe]
jwe
parents:
diff changeset
53 else
707b9396ad2c [project @ 1996-05-24 02:22:01 by jwe]
jwe
parents:
diff changeset
54 error ("index: expecting string arguments");
707b9396ad2c [project @ 1996-05-24 02:22:01 by jwe]
jwe
parents:
diff changeset
55 endif
707b9396ad2c [project @ 1996-05-24 02:22:01 by jwe]
jwe
parents:
diff changeset
56
707b9396ad2c [project @ 1996-05-24 02:22:01 by jwe]
jwe
parents:
diff changeset
57 endfunction