Rocksolid Light

News from da outaworlds

mail  files  register  groups  login

Message-ID:  

BOFH excuse #379: We've picked COBOL as the language of choice.


comp / comp.lang.lisp / Re: string search and assignment

SubjectAuthor
* Re: string search and assignmentB. Pym
`- Re: string search and assignmentKaz Kylheku

1
Subject: Re: string search and assignment
From: B. Pym
Newsgroups: comp.lang.lisp
Organization: A noiseless patient Spider
Date: Tue, 2 Jul 2024 19:38 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: string search and assignment
Date: Tue, 2 Jul 2024 19:38:09 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 69
Message-ID: <v61kut$1p0ar$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=iso-8859-1
Injection-Date: Tue, 02 Jul 2024 21:38:09 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="f8299da78adc737e104c9a85fb07d21d";
logging-data="1868123"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19PiyIJwHOkb8Hix3Excr6b"
User-Agent: XanaNews/1.18.1.6
Cancel-Lock: sha1:B+I9UJmQ2BS5zYkhwIEr6V0XNeA=
View all headers

Kent M. Pitman wrote:

> ... What i want to do is start at the beginning of the
> file( which I can do) and search for the word "entity".
> Once I find this, I want to assign the next word to a
> variable. ...
>
> Use WITH-OPEN-FILE to open the stream. It will let you specify
> a variable to which the stream is bound. The I/O routines all
> take a stream as argument. e.g.,
>
> (defun find-word-association-in-file (word file)
> (with-open-file (stream file :direction :input)
> (loop
> (let ((line (read-line stream nil nil)))
> (declare (type string line))
> (unless line (return nil))
> (let ((pos1 (position #\Space line :test #'char-equal)))
> (when (and pos1 (string-equal word line :end2 pos1))
> (let ((pos2 (position #\Space line
> :test (complement #'char-equal)
> :start pos1)))
> (when pos2
> (return (subseq line pos2
> (position #\Space line
> :test #'char-equal
> :start pos2)))))))))))
>
> Given a data file "delete-me.text" containing:
>
> FOO OOF
> BAR RAB XYZZY
> BAZ ZAB PLOVER PLUGH
> NUL NIL
>
> I find that:
>
> (find-word-association-in-file "FOO" "delete-me.text") => "OOF"
> (find-word-association-in-file "BAZ" "delete-me.text") => "ZAB"
> (find-word-association-in-file "NUL" "delete-me.text") => "NIL"
> (find-word-association-in-file "GEE" "delete-me.text") => NIL

Gauche Scheme

(use file.util) ;; file->string-list
(use srfi-13) ;; string-tokenize
(use srfi-14) ;; character-sets

;; Assumes that the "next word" has to be on the same line.
(define (find-word-association-in-file word file)
(any
(lambda (line)
(let* ((words (string-tokenize line char-set:letter))
(tail (member word words string=?)))
(and tail (pair? (cdr tail)) (cadr tail))))
(file->string-list file)))

(find-word-association-in-file "FOO" "delete-me.txt")
===>
"OOF"

(find-word-association-in-file "PLOVER" "delete-me.txt")
===>
"PLUGH"

(find-word-association-in-file "PLUGH" "delete-me.txt")
===>
#f

Subject: Re: string search and assignment
From: Kaz Kylheku
Newsgroups: comp.lang.lisp
Organization: A noiseless patient Spider
Date: Tue, 2 Jul 2024 23:23 UTC
References: 1
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: 643-408-1753@kylheku.com (Kaz Kylheku)
Newsgroups: comp.lang.lisp
Subject: Re: string search and assignment
Date: Tue, 2 Jul 2024 23:23:53 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 33
Message-ID: <20240702160629.519@kylheku.com>
References: <v61kut$1p0ar$1@dont-email.me>
Injection-Date: Wed, 03 Jul 2024 01:23:53 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="30e244ac737476d78edd2cd6194d0a15";
logging-data="1933073"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19rAK/NmkMvI5fXE72xQg5I5rAHJb17bpQ="
User-Agent: slrn/pre1.0.4-9 (Linux)
Cancel-Lock: sha1:prXaoXsGS/xXYCt6Rb/HA+8Ozeo=
View all headers

On 2024-07-02, B. Pym <No_spamming@noWhere_7073.org> wrote:
> Gauche Scheme
>
> (use file.util) ;; file->string-list
> (use srfi-13) ;; string-tokenize
> (use srfi-14) ;; character-sets

This is garbage. If it comes with the language, the language should
understand it without any import statement nonsense.

(At least a high-level, dynamic language. Medium-level, non-interactive
languages like Ada and Modula-2 have decent reasons for it.)

> ;; Assumes that the "next word" has to be on the same line.
> (define (find-word-association-in-file word file)
> (any
> (lambda (line)
> (let* ((words (string-tokenize line char-set:letter))
> (tail (member word words string=?)))
> (and tail (pair? (cdr tail)) (cadr tail))))
> (file->string-list file)))

TXR Lisp:

(defun find-following-word-in-file (path word)
(if-match @(some @(scan (@word @other . @nil)))
(mapcar (op tok #/\w+/) (file-get-lines path))
other))

--
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
Mastodon: @Kazinator@mstdn.ca

1

rocksolid light 0.9.8
clearnet tor