Rocksolid Light

News from da outaworlds

mail  files  register  groups  login

Message-ID:  

Don't Worry, Be Happy. -- Meher Baba


comp / comp.lang.lisp / Re: Searching a character within a line

SubjectAuthor
o Re: Searching a character within a lineB. Pym

1
Subject: Re: Searching a character within a line
From: B. Pym
Newsgroups: comp.lang.lisp
Organization: A noiseless patient Spider
Date: Sun, 7 Jul 2024 11:43 UTC
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: No_spamming@noWhere_7073.org (B. Pym)
Newsgroups: comp.lang.lisp
Subject: Re: Searching a character within a line
Date: Sun, 7 Jul 2024 11:43:16 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 49
Message-ID: <v6dv0h$aouc$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=iso-8859-1
Injection-Date: Sun, 07 Jul 2024 13:43:16 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="5c179f01f5b841e56caed336ca0031fe";
logging-data="353228"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19dcanfGysUdeJs/M0g9fCk"
User-Agent: XanaNews/1.18.1.6
Cancel-Lock: sha1:Ly7PRNrNLiprGvV72oxViSjLA8w=
View all headers

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

1

rocksolid light 0.9.8
clearnet tor