• Welcome to PlanetSquires Forums.
 

arrays, list in freebasic

Started by raymw, January 07, 2018, 06:16:46 PM

Previous topic - Next topic

raymw

Having just come back to freebasic, and firefly, and being a bit confused.
At the moment I'm stuck on setting values into an array. I've a list of two columns of numbers (100 in total), and hoped I could wrap something like  Dim Shared  xy(103) as Double  ={ 11.1820, 99.3837 ,
-1.3761, 100.0000, 
-13.9125, 99.0392,
lots more numbers...
23.5638, 97.2002,
11.1820, 99.3837  }


The values were derived from elsewhere, and formatted into this list. I think the fb array wants all the numbers on the same line, which is going to be more tricky to recognize errors in them, during the 'honing/development process. Anyone any ideas on how to create this array, without too much hassle.

Paul Squires

I believe you just need to end each line with the "line continuation" character which is an underscore.

Dim Shared  xy(103) as Double  ={ 11.1820, 99.3837 , _
-1.3761, 100.0000, _
-13.9125, 99.0392, _
lots more numbers...
23.5638, 97.2002, _
11.1820, 99.3837  }

Paul Squires
PlanetSquires Software
WinFBE Editor and Visual Designer

raymw

Thanks, Paul. That did the trick. Any idea how i can purge my single brain cell of javscript, C#, Python, and all the other rubbish I have in there?

Paul Squires

Paul Squires
PlanetSquires Software
WinFBE Editor and Visual Designer

raymw

right, having completed my latest FF/FB programming exercise, is there an easy way, a parser/whatever, that will format the resulting code into something pretty, e.g. consistent line indents for blocks of code, and so forth?

Paul Squires

There is nothing inside of FireFly itself that does automatic code formatting. It is not something that I have devoted any energy to in the past.
Paul Squires
PlanetSquires Software
WinFBE Editor and Visual Designer

raymw

another array problem that I have, this time in freebasic. How do I define an array of strings? I don't know the length of each string, nor the number of said strings. So far, the program compiles, then fails with an unknown error. I've attached a sub set of the code to demonstrate the problem.
dim shared x1 as double,y1 as double
dim shared op() as string      'list of g code statements
dim shared p as integer        'counter for op
       
  '------------------------------
sub ad(opline as string)    'adds line of g code to op
  op(p)=opline
    p=p+1
end sub
'-----------------------
sub getvalues()
      input "input x1 ",x1
      input "input y1 ",y1
end sub
'-----------------------
sub writecomments()
       ad( "(rectangle x1 = "+str(x1)+")")
      ad( "(rectangle y1 = "+str(y1)+")")
END sub
'-------------
sub showcode()
   dim k as Integer
   k=0
while   k<p
    print op(k)
    k= k+1
WEND
END sub
'---------------------------

'--------------------------
'main loop
p=0
getvalues()
writecomments()
showcode()

      print "done"
sleep


from what I've read in the fb help  my op() should give a variable length array - but as mentioned, it crashes when running. For the small example I've shown op(1) will work, but that is only an array of two small strings, so should I set the size to be larger than I might ever need, say op(100000)?

Johan Klassen

use redim to create variable length array, here's a very simple example

redim shared as string s(0 to 2)
s(0)="0"
s(1)="11"
s(2)="222"

for i as long=0 to 2
print s(i)
next

?"======================"

redim preserve s(0 to 4)

s(3)="3333"
s(4)="44444"

for i as long=0 to 4 'or for i as long=lbound(s) to ubound(s)
print s(i)
next

raymw

Thanks Johan, that seems to do it. A bit ugly as each time I added a string I redimed the array by +1, but works ok, so far.