Path: csiph.com!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail From: "WJ" Newsgroups: comp.lang.forth Subject: Re: string-stack.4th Date: Fri, 11 Mar 2016 06:24:35 -0000 (UTC) Organization: A noiseless patient Spider Lines: 83 Message-ID: References: <6aed5e5d-c9e2-42a3-b170-86e862d0f21f@googlegroups.com> <799b10b1-69a7-47ed-acdb-a791cbd4d248@googlegroups.com> <36f34016-ac87-4117-bff3-66bdc6e016d0@googlegroups.com> <56dd73a9$0$24031$e4fe514c@news.xs4all.nl> <4696cc36-ce3d-4786-b134-1da0a41f03c1@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Injection-Date: Fri, 11 Mar 2016 06:24:35 -0000 (UTC) Injection-Info: mx02.eternal-september.org; posting-host="a53f31a9c1e7010c521b7027319b6748"; logging-data="13837"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18xy5HAwoXGPGWIkoqWSOJO" User-Agent: XanaNews/1.18.1.6 X-Antivirus-Status: Clean X-Antivirus: avast! (VPS 151216-2, 12/16/2015), Outbound message Cancel-Lock: sha1:gNPD2Vx+6D1PaKrIdTyXGiLRsgI= Xref: csiph.com comp.lang.forth:45968 hughaguilar96@gmail.com wrote: > In the meantime, I upgraded the capability of string-stack.4th > a bit. I can do this now: > > s" foo:=bar" >$ ok > s" :=" >$ infix$ . -1 ok > extract$ ok > .$ bar ok > .$ foo ok > > INFIX$ returns a flag indicating if it found the pattern in > the string. The dot gave me -1 (TRUE) so I then used EXTRACT$ > to remove the infix and return the prefix and suffix that were > surrounding the infix. So, I parsed a little bit of Pascal > code there (in Pascal, := is an assignment). Don't forget that ":=" can be surrounded by whitespace. Ruby: " foo := bar ".split(":=").map(&:strip) ===> ["foo", "bar"] OCaml: #load "str.cma";; List.map String.trim (Str.split (Str.regexp ":=") " foo := bar ") ;; ===> ["foo"; "bar"] If you want a considerable amount of string-manipulation power, you'll have to implement a regular-expression engine. Example: find each integer in a string and increment it. Ruby: "9, 10, 11 I see, now that 1983 has come and gone". gsub(/\d+/){|s| s.to_i + 1} ===> "10, 11, 12 I see, now that 1984 has come and gone" OCaml: #load "str.cma";; Str.global_substitute (Str.regexp "[0-9]+") (fun s -> string_of_int ((int_of_string (Str.matched_string s)) + 1)) "9, 10, 11 I see, now that 1983 has come and gone";; ===> "10, 11, 12 I see, now that 1984 has come and gone" Instead of splitting a string, shatter it. In other words, keep the separating strings instead of discarding them. Ruby: "foo := bar; write 8*9; end".split(/([^\w]+)/) ===> ["foo", " := ", "bar", "; ", "write", " ", "8", "*", "9", "; ", "end"] OCaml: #load "str.cma";; open Str;; Str.full_split (Str.regexp "[^a-z0-9]+") "foo := bar; write 8*9; end" ;; ===> [Text "foo"; Delim " := "; Text "bar"; Delim "; "; Text "write"; Delim " "; Text "8"; Delim "*"; Text "9"; Delim "; "; Text "end"] -- From the New York Times of October 11, 1991, ... we learn that ... researchers at Boston University admitted that, "There is no question but that Dr. King plagiarized in the dissertation." ... "Dr. Martin Luther King, Jr." [Michael King] spent his last night on Earth having sexual intercourse with two women at the motel and physically beating and abusing a third. -- K. A. Strom