Re: Split a string and characters

Bernd Paysan <[email protected]> Sun, 04 Dec 2022 19:45:14 +0100
Newsgroups gmane.comp.lang.forth.gforth
Message-ID <2265783.ElGaqSPkdT@liubei>
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).

> 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.

-- 
Bernd Paysan
"If you want it done right, you have to do it yourself"
net2o id: kQusJzA;7*?t=uy@X}1GWr!+0qqp_Cn176t4(dQ*
https://net2o.de/
signature.asc (application/pgp-signature, 833 B)
-----BEGIN PGP SIGNATURE-----

iQIzBAABCAAdFiEERJ1NDqPQRwYnwBjr9y2Uk5MtoGcFAmOM6roACgkQ9y2Uk5Mt
oGe6OBAAk4z6gWF+AFK+mVVVtEYD1VKOVKLQXVd+Q2SzHeTaWUk5cWE7WXj5s93S
CTu78Y9t16aLZYvPejWMrv0j4abgm+TCuuoyM1qmyxJ7zZC3uF7/4l8OnJaiBrmd
umj7D9RDA4PyVw44xq1YumN4hwbgBnZCvsQfUvJhAmDadL0fVYwYSatmX5u6/qXF
SdLez46/xWmvuxzUXlaGEiBZO065OnEUMSK5oIRApoLZLDicyiXaJ2RXpqWqbpYV
7kX8OlY+QmBCHb7lsTr9rTUzzmz22IEIftoPEpy9F3HEnqy0iuJzYzZjVIa2mAf3
SN8UobrJTUuMxGaAzt2niasCA5CWDHXEkeOSmqn/o13De9e69m4O2ITcOej/Czas
zl+/CkTXygnYV/g1XGIOAKR8CiNWQ047IORZWNpS7W2LFLNhqS+TnmHUgt126j2O
DnKqIGDUIGYVjK3/OHGHHAIga4+ACdnYZ3CAlIqWq36wpdqAN7+sVI6eCBICoR3H
Ut0B7Ba1pr9mJ2KL95iKL2VNRGvvZE//M/OOiv4dpaVGT6ca7nPXj5vQSRf9Jukr
cH/p9YIwlfyp1fz1zOU/4j5rvQVLoboLUb2E6gDalo22Uc13tdTLZaE42PVCClgM
gmP5NxnKG0kBMgiRMZSNpSCEZOBJB6yLNqFghGcCvVY4ebNziy4=
=B/Bg
-----END PGP SIGNATURE-----