Re: How to calculate Character Position in String

Subject: Re: How to calculate Character Position in String
From: Brandon Ibach <bibach@xxxxxxxxxxxxxx>
Date: Mon, 5 Jun 2000 21:12:43 -0500
Quoting Roland Schopf <Roland.Schopf@xxxxxxxxxxx>:
> I have the follwing sgml:
> <table tabid="doc.123.T456"><title>Table Title Text</title>...</table>
> 
> and I want to generate somthing like:
> <P>Table 456: Table Title Text</P>
> 
> My only problem now is, how to get the position of the last dot, in order to
> cut the attribute-string with the substring function.
> 
   Here's a function that should be helpful.  It is based on Norm
Walsh's (find-first-char) function, which can be found in the DSSSL
Procedure Library at:
     http://www.mulberrytech.com/dsssl/dsssldoc/procedures/index.html
   The comments describe how it works, but in your case, you should be
able to get the value you want with something like:
     (let ((tabid (attribute-string "tabid" (current-node))))
       (substring tabid (find-char tabid "." -1 -1) (string-length tabid)))
or:
     (let* ((tabid (attribute-string "tabid" (current-node)))
            (period (find-char tabid "." -1 -1)) (dig "123456789"))
       (substring tabid (find-char tabid dig period) (string-length tabid)))

-Brandon :)

;; Finds nth character in string that is in chars (or not in chars,
;; if skip is true).  If n is negative, searches backwards.  Search
;; begins at pos.  If pos is unspecified, search begins at start for
;; positive n, or end for negative n.  A negative value for pos begins
;; the search at the (-pos)th character from the end of the string.
;; If no such characters are found, returns -1.
;;
(define (find-char string chars #!optional (pos #f) (n 1) (skip #f))
   (let ((len (string-length string)) (sp (or pos (if (< n 0) -1 0)))
         (dir (if (< n 0) -1 1)) (clist (string->list chars)))
     (let loop ((p (if (< sp 0) (+ len sp) sp)) (o (abs n)))
       (if (or (>= p len) (< p 0)) -1
           (let ((found (member (string-ref string p) clist)))
             (if (if skip found (not found)) (loop (+ p dir) o)
                 (if (< o 2) p (loop (+ p dir) (- o 1)))))))))


 DSSSList info and archive:  http://www.mulberrytech.com/dsssl/dssslist


Current Thread