Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7013715
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:25:37+00:00 2026-05-27T22:25:37+00:00

I tried to implement a word that produces a string from an array when

  • 0

I tried to implement a word that produces a string from an array when given a number on the stack in Forth.

My first naive attempt was:

create myarray s" Alpha" , s" Beta" , s" Charlie" ,

This was accepted, but it did not work as expected — myarray @ type produces inconsistent output (instead of my naive expectation that it might print “Alpha”).

When searching the web, I found in Gforth documentation that a string created with s" has a limited lifetime which means that my ansatz is bound to fail from the beginning. On the other hand, even arrays of regular objects seem to be not standardized according to Arrays in Forth section in Len’s Forth Tutorial.

<Update> Apparently, this is not a trivial problem with Forth. There are libraries on the web that implement missing string functionality: FFL (str module) and String Functions by Bernd Paysan. This is a good starting point, although it still requires work to go from there to an array of strings. </Update>

So how can I implement a word that returns a string from a given array?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T22:25:38+00:00Added an answer on May 27, 2026 at 10:25 pm

    To address parts of your code, s" leaves addr u on the stack, an address and the length of the string. , only stores one value so you won’t get the desired results that way. 2, might do it as that would store both of the stack items that represent the string. Once you have done that you need to get both values back too so 2@ is what you want.

    My rewrite would look like this:

    create myarray s" Alpha" 2, s" Beta" 2, s" Charlie" 2,
    
    \ Test
    myarray 2@ type Alpha **ok**
    

    Getting at the other elements of your array is a bit trickier. When you type myarray you get the address of the start of the data in that dictionary entry, and you can then use 2@ to get the the things that the first two addresses point to (which are the address and length of “Alpha”). If you want “Beta you need the next pair of addresses. So you can use

    myarray 2 cells + \ increment the address by two cells
    

    To get the addresses that point to “Beta” and so on. So in order to access “Beta” you would enter

    myarray 2 cells + 2@ type Beta **ok**
    

    I have tested this with gforth and it seems to all work, although I am not sure how to rigorously test for persistence.

    Your word would need to be able to do the address incrementing based on what is on the stack to start with. You might want to get into some more create does> stuff. I can give some pointers but I don’t want to spoil the fun of discovery.

    If I am skipping too many details of what this actually means just say, and I will try again.


    Maybe this is too crude, but I had a go at making a “string type” of sorts a while ago.

    : string                                    ( addr u "name" -- )
        create 2,                               \ add address and length to dict entry "name"
        does> dup cell+ @ swap @ ;              \ push addr u
    
    \ Example
    s" Some Words" string words **ok**
    words type Some Words **ok**
    

    It defines a word with a name of your choosing (in this case “words”) that will push length and start address of your string (in this case “some words”) when it is interpreted. As far as I know when the string is in a definition like this it is persistent.

    This doesn’t answer you question fully, but it might help.


    I have had another go at a persistent string, this one definitely allots memory within a dictionary entry and will be safe as long as that word exists. Before the string “type” only stored the address and length that s" created which is only any good until something else writes over that region of memory. This now copies the string from where s" creates it into a dictionary item called “name” where it is guaranteed to last as long as “name” itself.

    : string                                    ( addr u "name" -- )
        create                                  \ create dict entry called "name"
        dup >r here >r                          \ keep copies of string length and start of "name"'s memory
        dup 2 cells + allot                     \ allot memory for the number of chars/bytes of the string plus 2
                                                \ for the new addr u
        r@ 2 cells +                            \ Get the address two cells from the start the space for "name"
        swap cmove                              \ copy the string at addr u into the alloted space for "name"
    
        \ Now "name" looks like this: "name" -blank1- -blank2- "the text of the string at addr u"
        \ blank1 should be the address of the start of the the text = addr2 and blank2 should be u
    
        r@ dup 2 cells + swap !                 \ get the address of blank1, copy it, increment by 2 to get addr2
                                                \ and then store that in blank1
        r> cell+ r> swap !                      \ get address of blank1, increment to get address of blank2, then get u and
                                                \ store it in blank2
    
        \ Now "name" looks like this: "name" addr2 u "the text of the string at addr u"
    
        does> dup @ swap cell+ @ ;              \ push addr2 u
    

    For amusement, I thought I might show how little sense this makes without helpful formatting

    : string-no-comments         ( addr u "name" -- )
        create dup >r here >r dup 2 cells + allot r@
        2 cells + swap cmove r@ dup 2 cells + swap !
        r> cell+ r> swap ! does> dup @ swap cell+ @ ;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I once tried to implement Comet in PHP. Soon, I found that PHP is
I tried to implement a suggestion that came up in another question: Stackoverflow question
I tried to implement __concat__ , but it didn't work >>> class lHolder(): ...
I tried to implement XOR swap in python. x,y= 10,20 x,y,x = x^y,x^y,x^y print('%s
Anyone who has tried to implement RoutedCommands in WPF using M-V-VM has undoubtedly run
I've an WPF application where tried to implement MVVM pattern and Prism 2. I
I've searched the forum, and tried to implement the code in the threads I
I have been looking around for solutions, and tried to implement what is often
I am required to implement a functionality similar to SO voting. I tried to
I'm wondering how to implement what is stated in the title. I've tried something

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.