gforth
[Top][All Lists]
Advanced

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

Split a string and characters


From: Zelphir Kaltstahl
Subject: Split a string and characters
Date: Sun, 4 Dec 2022 12:11:18 +0000

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.

I read in the manual and found the following pages:

https://gforth.org/manual/String-words.html#index-_0024split_0028--addr-u-char-_002d_002d-addr1-u1-addr2-u2--_0029-gforth_002d0_002e7

But how do I put a mere character on the stack? Does that mean just putting the ASCII code of the character on the stack?

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
4split
4split
:1: Undefined word
4split<<<
Backtrace:
$7F6D56FD3A38 throw
$7F6D56FE9CB8 no.extensions
$7F6D56FD3CF8 interpreter-notfound1

$ 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
string-split
string-split
:1: Undefined word
string-split<<<
Backtrace:
$7FBB416E9A38 throw
$7FBB416FFCB8 no.extensions
$7FBB416E9CF8 interpreter-notfound1
~~~~

Even though the manual says: "|$split| ( /addr u char – addr1 u1 addr2 u2 /) gforth-0.7 “string-split”"

Not sure where my misunderstanding is.

https://rosettacode.org/wiki/Tokenize_a_string#Forth

Not sure how good the information is on Rosetta Code, but that seems to work as described there. I would still prefer to use an in-built word doing what $split claims to do: ( addr u char – addr1 u1 addr2 u2  ), because that is what I expected in the rest of the code. I already have a `score` word, which is supported to calculate the score of each line of the input, once the line is split into 2 strings. All I need to do is to split the string correctly.

Is it true, what Rosetta Code claims, that there is no standard way of splitting a string?

And why do I get an error, when I try to use the string split words from the GForth manual?

And how should I be splitting a string?

Regards,
Zelphir

--
repositories:https://notabug.org/ZelphirKaltstahl


reply via email to

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