User Tools

Site Tools


data_20without_20data

Data without DATA

By JGH, May-2006.
It is common for programs to put commonly used data in DATA statements which are then read into a set a variables at startup. A classic example is the names of months:

        DIM mon$(12)
        RESTORE
        FOR mon%=1 TO 12:READ mon$(mon%):NEXT mon%
        DATA Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec

This has a few immediate disadvantages:

  • The data is held in memory twice, in the program itself, and in mon$()
  • The DATA pointer is modified, unless LOCALised


You can avoid the DATA pointer being modified by doing the following:

        DIM mon$(12)
        mon$() = "","Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"

but the data is still held in memory twice.

An alternative for small bits of fixed data like this is to hold them in a string:

        DEF FNmon(mon%)=MID$("JanFebMarAprMayJunJulAugSepOctNovDec",mon%*3-2,3)

This has several advantages:

  • The data string only occurs in the memory once
  • The DATA pointer is not affected


This can even be done for data that at first sight doesn't look like fixed data:

        DEF FNmonth(mon%) \
        \ =MID$("JanuaryFebruaryMarchAprilMayJuneJulyAugustSeptemberOctoberNovemberDecember", \
        \ VALMID$("010816212629333743525967",mon%*2-1,2), \
        \ VALMID$("785534469788",mon%,1))

The first VALMID$ string is a series of initial start positions of the month name strings for each month. The second VALMID$ string is the length of each month name.

Note: the example functions only give valid results for valid month numbers.

This website uses cookies. By using the website, you agree with storing cookies on your computer. Also you acknowledge that you have read and understand our Privacy Policy. If you do not agree leave the website.More information about cookies
data_20without_20data.txt · Last modified: 2024/01/05 00:22 by 127.0.0.1