Re: Searching a character within a line

Liste des GroupesRevenir à cl lisp 
Sujet : Re: Searching a character within a line
De : No_spamming (at) *nospam* noWhere_7073.org (B. Pym)
Groupes : comp.lang.lisp
Date : 07. Jul 2024, 12:43:16
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <v6dv0h$aouc$1@dont-email.me>
User-Agent : XanaNews/1.18.1.6
Pierre R. Mai wrote:

(defun get-field-from-line (field line)
  "Returns a copy of the given field (0 based index) from the line.
Fields are separated by '|'.  If there are fewer fields in line than
field, nil is returned."
  (loop for start = 0 then (1+ position)
        for position = (and start (position #\| line :start start))
        repeat field
        when (null position)
        do (return nil)
        finally
        (return (subseq line start position))))

Gauche Scheme

(use srfi-13) ;; string-take string-index

(define (get-field-of-line index line)
  (let go ((i index) (s line))
    (let ((found (string-index s #\|)))
      (if (zero? i)
        (or (and found (string-take s found)) s)
        (and found (go (- i 1) (string-drop s (+ 1 found))))))))

(get-field-of-line 0 "|2|Dollars|23-dec-1999|Idaho")
  ===>
""

(get-field-of-line 1 "|2|Dollars|23-dec-1999|Idaho")
  ===>
"2"

(get-field-of-line 2 "|2|Dollars|23-dec-1999|Idaho")
  ===>
"Dollars"

(get-field-of-line 3 "|2|Dollars|23-dec-1999|Idaho")
  ===>
"23-dec-1999"

(get-field-of-line 4 "|2|Dollars|23-dec-1999|Idaho")
  ===>
"Idaho"

(get-field-of-line 5 "|2|Dollars|23-dec-1999|Idaho")
  ===>
#f


Date Sujet#  Auteur
7 Jul 24 o Re: Searching a character within a line1B. Pym

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal