gforth
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Split a string and characters


From: Bernd Paysan
Subject: Re: Split a string and characters
Date: Sun, 04 Dec 2022 19:45:14 +0100

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/

Attachment: signature.asc
Description: This is a digitally signed message part.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]