Writing into an array

Discussions about the BBC BASIC language, with particular reference to BB4W and BBCSDL
zeynel1
Posts: 21
Joined: Sun 15 May 2022, 11:35

Writing into an array

Post by zeynel1 »

Code: Select all

      FOR i=0 TO 21
        G=FN_CavendishG(B_RADIAN(i),T(i))
        PRINT "B()=";B(i);" " "B_RADIAN()= ";B_RADIAN(i); " " "T()=";T(i);" " "G=";G
      NEXT i
Let's say I have an array DIMensioned as G_AVERAGE(21), how can I write these 21 numbers into this array?

For now, I printed the results of the above function into a file and copied the results and tried to populate the array like this,

Code: Select all

G_AVERAGE() = 5.3497135E-10, 5.07629921E-10, 5.01164606E-10,........
But I got a "LINE TOO LONG" error and the file closed. Is there a function like APPEND that adds data to an array?

Thanks

zeynel1
Posts: 21
Joined: Sun 15 May 2022, 11:35

Re: Writing into an array

Post by zeynel1 »

I tried to use DATA but again I got the "line too long" error. And when I try to shorten the lines like this,

Code: Select all

      
      DATA=5.3497135E-10, 5.07629921E-10,            \
      \                   .01164606E-10, 5.07960059E-10, \
      \                  .58593233E-10, 5.03531378E-10,  \
      ......
The first digit of numbers on the subsequent lines are lost. I don't know why this is happening.

EDIT: Writing DATA like this

Code: Select all

      DATA 5.349E-10, 5.076E-10, 5.011E-10, 5.079E-10, 2.585E-10
      DATA 5.035E-10, 2.688E-10, 5.206E-10, 2.671E-10, 4.881E-10
      DATA 5.549E-10, 5.374E-10, 5.254E-10, 5.222E-10, 5.174E-10
      DATA 5.023E-10, 5.300E-10, 5.182E-10, 5.335E-10, 4.923E-10
      DATA 4.979E-10
 
solved this problem.
Last edited by zeynel1 on Mon 30 May 2022, 07:50, edited 2 times in total.

nvingo
Posts: 16
Joined: Sat 28 May 2022, 22:40

Re: Writing into an array

Post by nvingo »

zeynel1 wrote:
Mon 30 May 2022, 07:32
I tried to use DATA but again I got the "line too long" error. And when I try to shorten the lines like this,

Code: Select all

      
     DATA=5.3497135E-10, 5.07629921E-10,            \
      \                   .01164606E-10, 5.07960059E-10, \
      \                  .58593233E-10, 5.03531378E-10,  \
      ......
The first digit of numbers on the subsequent lines are lost. I don't know why this is happening.
Go back to your code in post#1
Each element in an array can be treated as an individual variable.
So to assign or read a value:
Array(1)=data: PRINT Array(1)
A$(1)="string"
A%(n)=integer
The number in brackets (index) can also be a variable/any numeric expression - often the variable of a FOR...NEXT loop.
Arrays can be multidimensional
DIM chessboard(8,8)
DIM rubixcube(3,3,3)
Index of 0 is allowed.

Code: Select all

DIM g_a(21)
      FOR i=0 TO 21
        G=FN_CavendishG(B_RADIAN(i),T(i))
        g_a(i)=G
        PRINT "B()=";B(i);" " "B_RADIAN()= ";B_RADIAN(i); " " "T()=";T(i);" " "G=";G
      NEXT i
  
 
And, use integers where appropriate
FOR i%=0 TO 21...NEXT i%
Started using BASIC circa 1981 with CP/M, Video Genie, Sinclair ZX81, Acorn Atom, and progressed with ZX Spectrum, BBC Micro and Sinclair QL, Cambridge Z88, DOS, Windows. Wrote A-level project using school's BBC Micro with dual 800K floppy drive.

Hated Moron
Posts: 131
Joined: Thu 07 Apr 2022, 10:10

Re: Writing into an array

Post by Hated Moron »

zeynel1 wrote:
Mon 30 May 2022, 06:21
But I got a "LINE TOO LONG" error and the file closed. Is there a function like APPEND that adds data to an array?
No, but you can simply use the line-continuation character to create one long 'logical' line from multiple 'physical' lines:

Code: Select all

G_AVERAGE() = 5.3497135E-10, 5.07629921E-10, 5.01164606E-10, ..., \
\   ..., \
\   ...
You can even put one value per line, which in some circumstances can result in clearer code, and gives you the option to label each value:

Code: Select all

G_AVERAGE() = 5.3497135E-10, \ first item
\             5.07629921E-10, \ second item
\             5.01164606E-10, \ third item
\   ... \ etc.
\   ... \
\   ...
The suggestion to use a loop which reads the values from DATA statements will work, but it is over-complicated and does not directly address the issue you encountered which was the 'Line too long' error.

Since the ability to initialise an array in one statement was added in BASIC 5 (1984 or thereabouts!) using DATA statements for this purpose is largely obsolete.
If you are unhappy with any aspect of this post, please report it to the forum administrator (click on the exclamation mark icon in the top-right corner) rather than complaining on the open forum or by private email.

nvingo
Posts: 16
Joined: Sat 28 May 2022, 22:40

Re: Writing into an array

Post by nvingo »

Hated Moron wrote:
Mon 30 May 2022, 08:44
Since the ability to initialise an array in one statement was added in BASIC 5 (1984 or thereabouts!) using DATA statements for this purpose is largely obsolete.
It seems from the OP that the 22 values (i=0 TO 21) are being calculated with the user-defined function FN_Cavendish
For now, I printed the results of the above function into a file and copied the results and tried to populate the array like this,
so having the values within program code, either as DATA or an array assignment statement, is redundant too.
Started using BASIC circa 1981 with CP/M, Video Genie, Sinclair ZX81, Acorn Atom, and progressed with ZX Spectrum, BBC Micro and Sinclair QL, Cambridge Z88, DOS, Windows. Wrote A-level project using school's BBC Micro with dual 800K floppy drive.

Hated Moron
Posts: 131
Joined: Thu 07 Apr 2022, 10:10

Re: Writing into an array

Post by Hated Moron »

nvingo wrote:
Mon 30 May 2022, 09:12
It seems from the OP that the 22 values (i=0 TO 21) are being calculated with the user-defined function FN_Cavendish
Yes, but the actual question he asked was "I got a "LINE TOO LONG" error and the file closed. Is there a function like APPEND that adds data to an array?" so that's what my reply addressed - if not for the benefit of the OP in his specific circumstances but for more general interest.

It could also be that FN_Cavendish is a very slow and/or large function that one does not want to run every time the program is executed, if all it does is create a set of 'constants'.
If you are unhappy with any aspect of this post, please report it to the forum administrator (click on the exclamation mark icon in the top-right corner) rather than complaining on the open forum or by private email.

zeynel1
Posts: 21
Joined: Sun 15 May 2022, 11:35

Re: Writing into an array

Post by zeynel1 »

nvingo wrote:
Mon 30 May 2022, 07:41

Code: Select all

        g_a(i)=G
 
Thanks, this was what I was looking for.

zeynel1
Posts: 21
Joined: Sun 15 May 2022, 11:35

Re: Writing into an array

Post by zeynel1 »

Hated Moron wrote:
Mon 30 May 2022, 08:44

You can even put one value per line, which in some circumstances can result in clearer code, and gives you the option to label each value:
Thanks, I'm using Richard Russell's SDLİDE, and pressing return on a long line deleted the first number on the second line. So, I assume it is important not to exceed the maximum line size while entering data. It's all well now.

Hated Moron
Posts: 131
Joined: Thu 07 Apr 2022, 10:10

Re: Writing into an array

Post by Hated Moron »

zeynel1 wrote:
Mon 30 May 2022, 11:35
I'm using Richard Russell's SDLİDE, and pressing return on a long line deleted the first number on the second line.
No, it was interpreted as a line number (as an initial numeric value always is at the start of a line) and if the line number is zero it is not displayed.

If you make an edit which results in the first item on a line being a number, it will be interpreted as a line number. As this is often not what you want, you need to be careful when making edits that could have this result.

If you are splitting a line into two using the line continuation character, the trick is to type two backslashes (\\) at the point where you want to split it, position the cursor (caret) between them, and then press Enter. That will guarantee that nothing is lost or misinterpreted, because the first character on the new line will be a \.

This is not specific to SDLIDE, the BBC BASIC for Windows editor behaves in exactly the same way (and has for more than twenty years)!
If you are unhappy with any aspect of this post, please report it to the forum administrator (click on the exclamation mark icon in the top-right corner) rather than complaining on the open forum or by private email.

Hated Moron
Posts: 131
Joined: Thu 07 Apr 2022, 10:10

Re: Writing into an array

Post by Hated Moron »

Not a big deal but if there was a way to fix this so that my font settings stays that would be nice.
Is it specifically the font that's not being saved/restored, or is it all the settings (e.g. window size, preferences, recent files, keyboard macros etc.)? If it's just the font, that's rather surprising.

In case the settings file has become corrupted, I would suggest you first try deleting it (the file is sdlide.ini in your @usr$ directory). That will inevitably cause all the settings to be reset to the defaults, when you next restart SDLIDE, so you will have to re-select your preferences.

For reference, here's an extract from my sdlide.ini file so you can see how it's formatted:

Code: Select all

maximized=0
windowwidth=996
windowheight=599
wheelspeed=3.0
colouring=-1
indentation=-1
lowercase=0
darkmode=0
unicode=-1
includeon=-1
includelab=-1
sortlist=-1
guifont="C:\Users\richa\OneDrive\bbcbasic\sdl32\source\lib\DejaVuSans",12
editfont="C:\Users\richa\OneDrive\bbcbasic\sdl32\source\lib\DejaVuSansMono",11
If you are unhappy with any aspect of this post, please report it to the forum administrator (click on the exclamation mark icon in the top-right corner) rather than complaining on the open forum or by private email.