BBC BASIC for Windows
Programming >> BBC BASIC language >> Assigning values to Structure variables
http://bb4w.conforums.com/index.cgi?board=language&action=display&num=1443354959

Assigning values to Structure variables
Post by Joe68 on Sep 27th, 2015, 11:55am

Hi All.

I wonder if anyone can help me with this - or tell me what I am doing wrong.
I can assign values to arrays as per the example below.
I thought I could do the same with a structure (I didn't find anything in other posts, Help file, Programmers' Wiki to say I couldn't).

Examples:
Code:
      DIM Env(2)
      Env(0)=5
      Env(1)=2
      Env(2)=8
      REM -- with an array I can also assign values like this...
      Env()=5,2,8 

Code:
      DIM Env{(2)min,setpoint,max}
      Env{(0)}.min=5
      Env{(1)}.min=2
      Env{(2)}.min=8
      REM -- but I can't do the same with a structure.

      Env{()}.min=5,2,8
      REM -- invokes 'Mistake' error message 


Any advice?
Or must I use Struct{(n)}.member=value ?

Thanks.
Joe.
Re: Assigning values to Structure variables
Post by DDRM on Sep 27th, 2015, 4:28pm

Hi Joe,

To quote from the Mikado: "The fact appears to be as you've recited": I also get "mistake" if I try to allocate all the equivalent members of a structure array at the same time.

I'm not really surprised: the numbers in a simple array follow each other in memory, so it's a simple matter to write them consecutively, but I suspect that the whole STRUCTURE's worth of data is stored as a block, so the equivalent members of consecutive structures in an array will not be consecutive, making an equivalent function a lot more difficult.

Just because something is not explicitly forbidden it doesn't mean it will work ;-) I think you'll need to use the format you suggest at the end of your post - though you could "automate" it to some extent using a loop and a DATA statement.

Best wishes,

D
Re: Assigning values to Structure variables
Post by Joe68 on Sep 27th, 2015, 7:06pm

Thanks DDRM for your reply.

As an amateur / hobbyist I appreciate insights from more experienced programmers.

As you so correctly state, "just because its not explicitly forbidden doesn't mean it'll work". wink But I thought that maybe I just had the syntax wrong or a bracket in the wrong place.

I was hoping to avoid DATA statements and associated RESTOREs - I already have plenty data in my program, but loops, READ and DATA are the only way to go.

Cheers.
Joe
Re: Assigning values to Structure variables
Post by Zaphod on Sep 27th, 2015, 10:55pm

If you don't want more data statements you could assign the data to an array in one line as you hoped to do for the structure and then transfer it to the structure with a loop.
Re: Assigning values to Structure variables
Post by Joe68 on Oct 10th, 2015, 11:57am

Hi Zaphod.

Thanks for your reply.

I am assuming you mean something along the lines of the following:
Code:
      DIM envmin(2)
      envmin()=1,2,3

      DIM Env{(2)min,sp,max}
      FOR i=0 TO 2
        Env{(i)}.min=envmin(i)
        PRINT Env{(i)}.min
      NEXT 


That's an approach I had not considered, I'll see how it works in my program and if it will affect the consistency of it (as I said, I have a lot of other data statements for other parameters).

Cheers.
Joe