• Re: string search and assignment

    From B. Pym@21:1/5 to Kent M. Pitman on Tue Jul 2 19:38:09 2024
    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Kaz Kylheku@21:1/5 to B. Pym on Tue Jul 2 23:23:53 2024
    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: @[email protected]

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From B. Pym@21:1/5 to B. Pym on Thu Jun 26 21:07:06 2025
    B. Pym wrote:

    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

    Scheme

    (define (find-word-association-in-file word file)
    (with-input-from-file file
    (lambda()
    (let go ((s (read)))
    (cond ((eof-object? s) #f)
    ((equal? s word) (read))
    (#t (go (read))))))))

    (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 'PLOVER "delete-me.text")
    ===>
    PLUGH

    (find-word-association-in-file 'OOF "delete-me.text")
    ===>
    BAR

    (find-word-association-in-file 'NOWHERE "delete-me.text")
    ===>
    #f

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)