none
READ and DATA Usage

    Question

  • What is the Small Basic equivalent to:-

    10 FOR I = 1 TO 3

    20  READ A$

    30 NEXT

    100 DATA "Fred", "Gretel", "Hansel"

    This is common usage in other versions of Basic.  Cannot find equivalent in Small Basic in the forums or in the documentation,

    Chris

    Friday, March 03, 2017 8:59 PM

Answers

  • I remember in the early days of the BASIC, I used much READ and DATA. And I usually added to the end of DATA, the value - 1, which indicated to the program that we had reached the end of the values. Also the command RESTORE to put the list at the beginning of the values.

    It looked like this:

    10 FOR T = 1 TO 3

    20 READ A$

    25 IF A$="-1" THEN GOTO 80

    28 PRINT A$

    35 NEXT T

    80 REM end of data

    90 PRINT "End of data"

    95 STOP

    100 DATA "Fred", "Gretel", "Hansel","-1"

    '

                      With Small Basic , the translation would be this:

    '

    name = "1=Fred;2=Gretel;3=Hansel;"

    '

    For i = 1 To Array.GetItemCount (name)

    textwindow.WriteLine (name[i])

    endfor

    '






    • Edited by YLedEditor Saturday, March 04, 2017 3:40 PM
    • Marked as answer by Franganistan Saturday, March 04, 2017 5:43 PM
    Saturday, March 04, 2017 3:35 PM
    Answerer
  • Small Basic can do the counting of the data itself by a slightly more complex program:

    data="Fred;Gretel;Hansel"
    pos=Text.GetIndexOf(data,";")
    While pos>0
      i=i+1
      name[i]=Text.GetSubText(data,1,pos-1)
      data=Text.GetSubTextToEnd(data,pos+1)
      pos=Text.GetIndexOf(data,";")
    EndWhile
    i=i+1
    name[i]=data
    
    For i=1 To Array.GetItemCount(name)
      TextWindow.WriteLine(i+" "+name[i])
    EndFor
    It is also possible to another character for the ";" e.g  "," or even space " "


    Jan [ WhTurner ] The Netherlands


    • Edited by WhTurner33Editor Sunday, March 05, 2017 10:16 AM other separation added
    • Marked as answer by Franganistan Tuesday, March 07, 2017 5:57 AM
    Sunday, March 05, 2017 10:14 AM
    Answerer
  • to create arrays from list is the most simple way to use ldtext.split command

    instead of A = "1=Fred;2=Gretel;3=Hansel;"

    just writea=ldtext.split("Fred Gretel Hansel" " ")

    • Marked as answer by Franganistan Tuesday, March 14, 2017 8:23 PM
    Tuesday, March 14, 2017 1:31 PM

All replies

  • Simply use

    A = "1=Fred;2=Gretel;3=Hansel;"

    and see the Array class in SmallBasic

    PS: Is this  Julius Schärf - 'Basic für Anfänger' (~1981, red book) ?

    OUT OF DATA IN 20  :-)

    Friday, March 03, 2017 9:36 PM
    Answerer
  • Thank you.  I'll look up the Array class.  As for Julius ...., no, I am trying to convert a 1982 QBasic program to SB.

    Saturday, March 04, 2017 3:35 AM
  • I remember in the early days of the BASIC, I used much READ and DATA. And I usually added to the end of DATA, the value - 1, which indicated to the program that we had reached the end of the values. Also the command RESTORE to put the list at the beginning of the values.

    It looked like this:

    10 FOR T = 1 TO 3

    20 READ A$

    25 IF A$="-1" THEN GOTO 80

    28 PRINT A$

    35 NEXT T

    80 REM end of data

    90 PRINT "End of data"

    95 STOP

    100 DATA "Fred", "Gretel", "Hansel","-1"

    '

                      With Small Basic , the translation would be this:

    '

    name = "1=Fred;2=Gretel;3=Hansel;"

    '

    For i = 1 To Array.GetItemCount (name)

    textwindow.WriteLine (name[i])

    endfor

    '






    • Edited by YLedEditor Saturday, March 04, 2017 3:40 PM
    • Marked as answer by Franganistan Saturday, March 04, 2017 5:43 PM
    Saturday, March 04, 2017 3:35 PM
    Answerer
  • Small Basic can do the counting of the data itself by a slightly more complex program:

    data="Fred;Gretel;Hansel"
    pos=Text.GetIndexOf(data,";")
    While pos>0
      i=i+1
      name[i]=Text.GetSubText(data,1,pos-1)
      data=Text.GetSubTextToEnd(data,pos+1)
      pos=Text.GetIndexOf(data,";")
    EndWhile
    i=i+1
    name[i]=data
    
    For i=1 To Array.GetItemCount(name)
      TextWindow.WriteLine(i+" "+name[i])
    EndFor
    It is also possible to another character for the ";" e.g  "," or even space " "


    Jan [ WhTurner ] The Netherlands


    • Edited by WhTurner33Editor Sunday, March 05, 2017 10:16 AM other separation added
    • Marked as answer by Franganistan Tuesday, March 07, 2017 5:57 AM
    Sunday, March 05, 2017 10:14 AM
    Answerer
  • @Franganistan

    Basically in your given sample, 'READ' only fetches the respective 'DATA' value (name string) and assigns it to a variable, without any further processing or output.

    Sunday, March 05, 2017 9:16 PM
    Answerer
  • to create arrays from list is the most simple way to use ldtext.split command

    instead of A = "1=Fred;2=Gretel;3=Hansel;"

    just writea=ldtext.split("Fred Gretel Hansel" " ")

    • Marked as answer by Franganistan Tuesday, March 14, 2017 8:23 PM
    Tuesday, March 14, 2017 1:31 PM