Re: Split a string and characters
Zelphir Kaltstahl <[email protected]> Sun, 4 Dec 2022 19:15:33 +0000
| Newsgroups | gmane.comp.lang.forth.gforth |
|---|---|
| Message-ID | <[email protected]> |
On 12/4/22 19:45, Bernd Paysan wrote: > Am Sonntag, 4. Dezember 2022, 13:11:18 CET schrieb Zelphir Kaltstahl: >> Hello GForth users! >> >> Forth beginner here. I have a little program, in which I read a file line by >> line using a line-buffer as follows: >> >> ~~~~ >> >> : input-filename s" input" ; >> >> 256 Constant max-bytes-line >> Create line-buffer max-bytes-line 2 + allot >> input-filename r/o open-file throw Value puzzle-input-handle >> >> : total-score ( c-addr length -- ??? ) >> >> 0 \ accumulator value for score >> begin puzzle-input-handle file-eof? not >> while >> \ read lines until the file has been completely processed >> line-buffer max-bytes-line puzzle-input-handle read-line throw >> >> \ check that everything was read OK from the line buffer >> swap dup 0 > rot and >> >> if >> line-buffer swap \ acc c-addr length >> ... >> ~~~~ >> >> It took me quite a while to figure out how to read a file line by line and >> do something with each line, but ultimately I managed to find this way : ) >> Would be great, if there were more readable examples out there or in the >> manual though. >> >> Having the line in line-buffer, I now need to split the string at a space >> character: >> >> ~~~~ >> s" " >> ~~~~ >> >> But that is another string and I am not sure that qualifies as a character. > BL is the word you are looking for, the one that returns a space character. > > And you want to > > require string.fs > > in gforth-0.7.3 to get $SPLIT defined (or start gforth with string.fs as > argument). Ah, thank you! That works! >> Also $split is not available in GForth 0.7.3 and neither is string-split: >> >> ~~~~ >> $ gforth >> Gforth 0.7.3, Copyright (C) 1995-2008 Free Software Foundation, Inc. >> Gforth comes with ABSOLUTELY NO WARRANTY; for details type `license' >> Type `bye' to exit > gforth-0.7.3> ./gforth string.fs > Gforth 0.7.3, Copyright (C) 1995-2008 Free Software Foundation, Inc. > Gforth comes with ABSOLUTELY NO WARRANTY; for details type `license' > Type `bye' to exit > s" foo bla" bl $split ok > type bla ok > type foo ok > > The Gforth manual on gforth.org is the upcoming Gforth 1.0 manual, so it > contains features that weren't available in Gforth 0.7.3, or only available as > loadable files. OK, I will keep that in mind : ) Thank you! Regards, Zelphir -- repositories: https://notabug.org/ZelphirKaltstahl