DATA

New to FreeBASIC? Post your questions here.
TurtleProgrammer
Posts: 37
Joined: Jan 26, 2017 7:54

DATA

Postby TurtleProgrammer » Mar 07, 2017 16:48

Is there any way to use select case or something to correspond with integers in DATA lines?

For example:

say I have

DATA 1,3,1,5,1,1,1,5,3,2,3,1

can I use something like:

Select Case FieldCorresponding
case 1:
grass
case 2:
snow
case 3:
mud
case 4:
sand
case 5:
cement
End Select
MrSwiss
Posts: 3635
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: DATA

Postby MrSwiss » Mar 07, 2017 17:07

There is a bit more effort needed:

Code: Select all

DATA 1,3,1,5,1,1,1,5,3,2,3,1

Dim As String texture(1 To 5) ' whatever size needed
Dim As Byte   rByte
 
' initialize array of string
For ReadIndex As UInteger = 1 To 5
    Read rByte    ' get a DATA Byte
    Select Case As Const rByte  ' recode straight away
        Case 1 : texture(ReadIndex) = "grass"
        Case 2 : texture(ReadIndex) = "snow"
        Case 3 : texture(ReadIndex) = "mud"
        ...    ' extend as needed
    End Select
Next
MrSwiss
Posts: 3635
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: DATA

Postby MrSwiss » Mar 07, 2017 17:41

For small array's, you can also initialize it, straight away:

Code: Select all

Dim As String texture(1 To 5) => { "grass", "snow", "mud", "sand", "cement" }
adele
Posts: 47
Joined: Jun 13, 2015 19:33

Re: DATA

Postby adele » Mar 08, 2017 7:26

Just another idea.


Code: Select all

'Corresponding.bas

' the "DATA" could be read from a file as well...
DATA "1,3,1,5,1,1,1,5,3,2,3,1,0" ' added "0" as ´stopper´ :) Adi

Dim myStr As String
Read mystr ' = DATA

Dim FieldCorresponding As Long

Print MyStr

 For i As Integer=1 To Len(MyStr) Step 2
    ' expl.. VAL stops converting as soon  as a nun-numeric
    '  character is encountered. NonValid always is 0=zero
    '  so better start NOT at 0(zero), but with 1 as the
    '  first valid choice
'REM'    Print i & " : " & Mid(myStr,i,2) & " ";

    FieldCorresponding=Val(Mid(myStr,i))
   
   ' can I use something like:
   
      Select Case FieldCorresponding
         Case 1:
         Print "grass"
         Case 2:
         Print "snow"
         Case 3:
         Print "mud"
         Case 4:
         Print "sand"
         Case 5:
         Print "cement"
'         default:
         Case Else:
         Print "n/a:" & i & "==" & FieldCorresponding
      End Select
 Next i
sleep


To watch what`s going on "behind", uncomment the line 18 (??) == 'REM'

Adi
leopardpm
Posts: 1795
Joined: Feb 28, 2009 20:58

Re: DATA

Postby leopardpm » Mar 09, 2017 12:14

what?
sancho2
Posts: 547
Joined: May 17, 2015 6:41

Re: DATA

Postby sancho2 » Mar 10, 2017 7:23

Adele's code shows an alternate method of reading the data.
It converts the data statement integers into one comma delimited string and then walks through the string.
If you uncomment the rem line you can see each character as it is processed.
TurtleProgrammer
Posts: 37
Joined: Jan 26, 2017 7:54

Re: DATA

Postby TurtleProgrammer » Mar 12, 2017 3:58

I think I already know the answer to this question, but can I change variable's in a DATA statement during runtime?

Say: DATA "1,3,1,1,1,4,2" to DATA "1,2,2,4,2,1,2"

If I remember correctly I can't but I just thought I'd ask anyway.
MrSwiss
Posts: 3635
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: DATA

Postby MrSwiss » Mar 12, 2017 15:59

What you're having in DATA are Constants (which are then, read into variables).
So, no changes to anything in DATA possible, at run-time.
adele
Posts: 47
Joined: Jun 13, 2015 19:33

Re: DATA; change DATA variables

Postby adele » Mar 12, 2017 18:49

Hi,

TurtleProgrammer wrote:... but can I change variable's in a DATA statement during runtime?

Say: DATA "1,3,1,1,1,4,2" to DATA "1,2,2,4,2,1,2"


try:

Code: Select all

' read statements, just an example

restore  D_LABEL1:   
   read i1,i2,i3,i4,i5,i7 ' or whatever

restore  D_LABEL2:   
   read i1,i2,i3,i4,i5,i7 ' or whatever

' other code....

' please don`t run into this "code"...   
' the DOC says this might be dangerous
 D_LABEL1:   
 DATA 1,3,1,1,1,4,2
 
 D_LABEL2:
 DATA "1,2,2,4,2,1,2"
 


The coding style with DATA is, let us say, _very_ traditional :)
Nevertheless, in some cases more readable than other constructs

Adi

Return to “Beginners”

Who is online

Users browsing this forum: No registered users and 6 guests