Author Topic: reading data values  (Read 2554 times)

Offline Paul Smith

  • Mr. Polyvector
  • ***
  • Posts: 109
    • View Profile
reading data values
« on: 2014-Jul-12 »
Hi all
Never had to  use DATA storage in my code so far, however now i need to store 255 static  values between 0 and 9
i also need to  read the value at different positions e.g. position 10 = 0 or position 220 = 6
I've looked at help file and examples for data,array etc (some to complicated for me)
came up with this test

GLOBAL a
STARTDATA keys:
DATA 3, 3, 5, 5, 8, 1, 3, 9, 8, 0, 5, 9, 3, 9, 4, 1, 1, 0, 9, 4, 3, 0, 2, 2, 8, 4 // will have 255 values
ENDDATA

RESTORE keys
WHILE TRUE
READ a
PRINT a, 100,100
SHOWSCREEN
KEYWAIT
WEND

this will read the numbers in order
have no idea how to make it go to postion 50 or 12 and read data.

Anyone have any better ways, or  pointers

Cheers



Amstrad CPC 6128, ATARI STE.
Acer  SW5-173 & AMD FX 8350/1050Ti

MrTAToad

  • Guest
Re: reading data values
« Reply #1 on: 2014-Jul-12 »
You can only read from the beginning of DATA - this means that if you want to skip any you, need something like :

Code: GLBasic [Select]
FUNCTION skipData%:amount%=0
LOCAL s%

RESTORE keys

WHILE amount%>0
READ s%
WEND
ENDFUNCTION
 

Unfortunately there is no easy way to detect if all the data has been skipped.

Offline Paul Smith

  • Mr. Polyvector
  • ***
  • Posts: 109
    • View Profile
Re: reading data values
« Reply #2 on: 2014-Jul-12 »
couldn't get it working with it in a function( not you just my poor programming skills)
but your example made me understand it just like doing the same thing on a Amstrad CPC.
so I came up with this

GLOBAL amount, s
STARTDATA keys:
DATA 3, 3, 5, 5, 8, 1, 3, 9, 8, 0, 5, 9, 3, 9, 4, 1, 1, 0, 9, 4, 3, 0, 2, 2, 8, 4 // will have 255 values
ENDDATA

amount = 12

RESTORE keys

FOR t = 1 TO amount
READ s
NEXT
PRINT s,100,100
SHOWSCREEN
KEYWAIT

easy when you start to understand, Now doubt I'll be back when i forget how SHOWSCREEN works.  hehe

Just off topic MrTAToad is it you who wrote the GLBasic Programmers Reference Guide? if so do you still sell this and how much?

thanks for the help

Paul
Amstrad CPC 6128, ATARI STE.
Acer  SW5-173 & AMD FX 8350/1050Ti

MrTAToad

  • Guest
Re: reading data values
« Reply #3 on: 2014-Jul-12 »
I did write it.  And its now free!

It used to be in the forums, but isn't available any more here unfortunately, so I need to find it and upload it elsewhere.

Just need to find the files first...
« Last Edit: 2014-Jul-12 by MrTAToad »

Offline Paul Smith

  • Mr. Polyvector
  • ***
  • Posts: 109
    • View Profile
Re: reading data values
« Reply #4 on: 2014-Jul-12 »
excellent, cant wait to read it when its available.
Amstrad CPC 6128, ATARI STE.
Acer  SW5-173 & AMD FX 8350/1050Ti

Offline MrPlow

  • Prof. Inline
  • *****
  • Posts: 1536
    • View Profile
Re: reading data values
« Reply #5 on: 2014-Jul-12 »
Hi Paul

I would use array and index value to lookup your data.

Comp:
Speccy-48k, Speccy-128k, Amigas, PCs

Offline spacefractal

  • Community Developer
  • Prof. Inline
  • ******
  • Posts: 3682
    • View Profile
    • Space Fractal
Re: reading data values
« Reply #6 on: 2014-Jul-12 »
me too would uses a array too. This so its dosent needs to look for the required data. Howover data can still been used for "loading" thing of course.

Im have not used them very much, pretty much im did only used them for reading various test resolutions when testing under Windows.
Greedy Mouse - Karma Miwa - Spot Race - CatchOut - Android Extras - is on a vacation trip, home before end of few days in jan.

MrTAToad

  • Guest
Re: reading data values
« Reply #7 on: 2014-Jul-12 »
Its on my website now : http://triority.uk/books.html

Offline Paul Smith

  • Mr. Polyvector
  • ***
  • Posts: 109
    • View Profile
Re: reading data values
« Reply #8 on: 2014-Jul-13 »
Thanks for putting the Books online, will give them a read shortly. cant believe its Free.

arrays may be the the best option, but I have zero experience using them, i'm old fashioned and still use old programming techniques
with new bits added once I understand them.
Amstrad CPC 6128, ATARI STE.
Acer  SW5-173 & AMD FX 8350/1050Ti

Offline erico

  • Community Developer
  • Prof. Inline
  • ******
  • Posts: 4231
    • View Profile
    • Portfolio
Re: reading data values
« Reply #9 on: 2014-Jul-13 »
...
arrays may be the the best option, but I have zero experience using them, i'm old fashioned and still use old programming techniques
with new bits added once I understand them.

So am I. But you should not have any problems with arrays, they are quite simple to understand.
Reading the manual about it should be enough to pick it up.

Here is my take:

Normal variables would be like: a1=10, a2=3, a3=22,etc
But it would be hard to access them by their ordered number as it is on its own names.

So you need a 1 dimension array, which is pretty much the same but you have a variable for the indexed number.
So let´s say you first dimension your variable when you declare it like DIM a[254]
Now you have 255 (from 0 to 254) variables named a[0], a[1], a[2], etc, so you would go by a[0]=10, a[1]=3, a[2]=22, etc
So it is pretty much the same as my first example but with a difference, you can have a variable placed into the index, for example a[z].
This way you can go through checking the whole 255 variables with a simple for/next loop, like:
Code: GLBasic [Select]
for z=0 to 254
    if a[z]=5 then do the macarena
next

I hope it helps as I think that is best way to go on your code.

Another way of understanding arrays, and here I mean 2 dimension arrays, is to picture a chess board and consider the indexes as the x,y coordinates.
Like a[x,y]. I used such array to do a board game once, its board was supposed to hold 12 different values each variable and the size was 12x12 blocks.
I used DATA to read the map information into the array.
Here is the code snipet:
Code: GLBasic [Select]
//      VARIABLES
        GLOBAL c                                                                                                        // DECLARE X COUNTER VARIABLE
        GLOBAL d                                                                                                        // DECLARE Y COUNTER VARIABLE
        GLOBAL m[]                                                                                                      // DECLARE BOARD VARIABLE
        DIM m[12][12]                                                                                           // DIMENSION BOARD VARIABLE

//      LOAD MAP > BOARD
        RESTORE MAPA                                                                                            // load board
        FOR d=0 TO 11                                                                                           // generate board data into array
                FOR c=0 TO 11
                        READ m[c][d]
                NEXT
        NEXT

//      PRINT BOARD
        FOR d=0 TO 11                                                                                           // DRAW MAP / TABULEIRO
                FOR c=0 TO 11
                        PRINT m[c][d],c*20,d*20                                                         // draw BOARD
                NEXT
        NEXT

        SHOWSCREEN
        MOUSEWAIT


STARTDATA MAPA:

        DATA  1, 1, 1, 0, 0, 2, 2, 0, 0, 3, 3, 3
        DATA  1, 1, 1, 0, 0, 2, 2, 0, 0, 3, 3, 3
        DATA  1, 1, 1, 0, 0, 0, 0, 0, 0, 3, 3, 3
        DATA  0, 0, 0, 4, 4, 4, 0, 5, 5, 0, 0, 0
        DATA  0, 0, 0, 4, 4, 4, 0, 5, 5, 0, 0, 0
        DATA  6, 6, 0, 0, 0, 0, 0, 5, 5, 0, 7, 7
        DATA  6, 6, 0, 8, 8, 0, 0, 0, 0, 0, 7, 7
        DATA  0, 0, 0, 8, 8, 0, 9, 9, 9, 0, 0, 0
        DATA  0, 0, 0, 8, 8, 0, 9, 9, 9, 0, 0, 0
        DATA 10,10,10, 0, 0, 0, 0, 0, 0, 11,11,11
        DATA 10,10,10, 0, 0,12,12, 0, 0, 11,11,11
        DATA 10,10,10, 0, 0,12,12, 0, 0, 11,11,11

ENDDATA

hope this all helps out ;)
« Last Edit: 2014-Jul-13 by erico »

Offline Paul Smith

  • Mr. Polyvector
  • ***
  • Posts: 109
    • View Profile
Re: reading data values
« Reply #10 on: 2014-Jul-13 »
Cheers erico for taking the time to explain this, Had an idea on the working but not the syntax structure.



Amstrad CPC 6128, ATARI STE.
Acer  SW5-173 & AMD FX 8350/1050Ti