Initialising an array with many empty elements

New to FreeBASIC? Post your questions here.
fzabkar
Posts: 154
Joined: Sep 29, 2018 2:52
Location: Australia

Initialising an array with many empty elements

Post by fzabkar »

Is there a less ugly way to initialise an array with many empty elements? (code edited for brevity)

I have looked at Enum, Read/Data, and simple assignments of the form sAttnam( n ) = "text_n".

Code: Select all

Dim sAttnam ( 1 To 255 ) As Const String => _
    { _
    "Raw Read Error Rate", _                    '1
    "Throughput Performance", _                 '2
    "Spin Up Time", _                           '3
    "Start/Stop Count", _                       '4
    "Reallocated Sector Count", _               '5
    "Read Channel Margin", _                    '6
    "Seek Error Rate", _                        '7
    "Seek Time Performance", _                  '8
    "Power-On Hours Count", _                   '9
    "Spin Retry Count", _                       '10
    "Drive Calibration Retry Count", _          '11
    "Drive Power Cycle Count", _                '12
    "Soft Read Error Rate", _                   '13
    _
     "","","","","","","","","","","","","","","","","","","","", _     ' 20 null strings per line
     "","","","","","","","","","","","","","","","","","","","", _
     "","","","","","","","","","","","","","","","","","","","", _
     "","","","","","","","","","","","","","","","","","","","", _
     "","","","","","","","","","","","","","","","","","","","", _
     "","","","","","","","","","","","","","","","","","","","", _
     "","","","","","","","","","","","","","","","","","","","", _
     "","","","","","","","","","","","","","","","","","","","", _
     "","","","","","","","","", _
    _
    "SATA Downshift Error Count", _             '183
    "End to End Error Det/Corr Count", _        '184
...
    _ 
    "Free Fall Sensor", _                       '254
    _
     "" _
    }
badidea
Posts: 2591
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: Initialising an array with many empty elements

Post by badidea »

You could put the error codes and description in a separate (text) file:

1, "Raw Read Error Rate"
2, "Throughput Performance"
254, "Free Fall Sensor"


And read this at start and fill each listed error.
jj2007
Posts: 2326
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: Initialising an array with many empty elements

Post by jj2007 »

This works (but badidea's solution is better):

Code: Select all

Dim sAttnam ( 1 To 255 ) As String => _
    { _
    "Raw Read Error Rate", _                    '1
    "Throughput Performance", _                 '2
    "Spin Up Time", _                           '3
    "Start/Stop Count", _                       '4
    "Reallocated Sector Count", _               '5
    "Read Channel Margin", _                    '6
    "Seek Error Rate", _                        '7
    "Seek Time Performance", _                  '8
    "Power-On Hours Count", _                   '9
    "Spin Retry Count", _                       '10
    "Drive Calibration Retry Count", _          '11
    "Drive Power Cycle Count", _                '12
    "Soft Read Error Rate"_                   '13
    }
sAttnam(183)="SATA Downshift Error Count"
sAttnam(184)="End to End Error Det/Corr Count"
sAttnam(254)="Free Fall Sensor"
Print sAttnam(1)
Print sAttnam(2)
Print sAttnam(14)	' empty
Print sAttnam(254)
Sleep
fzabkar
Posts: 154
Joined: Sep 29, 2018 2:52
Location: Australia

Re: Initialising an array with many empty elements

Post by fzabkar »

Thanks. Both ideas had occurred to me, but they seemed less appealing than my eventual approach (because I wanted to avoid creating/loading the constants at runtime).

I had rejected the following idea:

Code: Select all

Dim attnam ( 1 To 255 ) As String
Dim numatts As UShort = 56        ' there are 56 attributes that need to be initialised, the rest are empty.

Data   1, "Raw Read Error Rate"
Data   2, "Throughput Performance"
Data   3, "Spin Up Time"
Data   4, "Start/Stop Count"
…

' read attribute strings into an array

For i = 1 to numatts
	Read id, attnam( id )
Next i
jj2007
Posts: 2326
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: Initialising an array with many empty elements

Post by jj2007 »

fzabkar wrote:I wanted to avoid creating/loading the constants at runtime
Loading a textfile of that length takes microseconds. If you want to avoid sending the file to your clients, you can embed it as a resource.
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Initialising an array with many empty elements

Post by dodicat »

You have a constant string to fill?
Just do it pedantically as your first post, you can shortcut a little.

Code: Select all

#define i "",

Dim sAttnam ( 1 To 255 ) As Const String => _
    { _
    "Raw Read Error Rate", _                    '1
    "Throughput Performance", _                 '2
    "Spin Up Time", _                           '3
    "Start/Stop Count", _                       '4
    "Reallocated Sector Count", _               '5
    "Read Channel Margin", _                    '6
    "Seek Error Rate", _                        '7
    "Seek Time Performance", _                  '8
    "Power-On Hours Count", _                   '9
    "Spin Retry Count", _                       '10
    "Drive Calibration Retry Count", _          '11
    "Drive Power Cycle Count", _                '12
    "Soft Read Error Rate", _                   '13
    i i i i i i i i i i i i i i i i i i i i _
    i i i i i i i i i i i i i i i i i i i i _
    i i i i i i i i i i i i i i i i i i i i _
    i i i i i i i i i i i i i i i i i i i i _
    i i i i i i i i i i i i i i i i i i i i _
    i i i i i i i i i i i i i i i i i i i i _
    i i i i i i i i i i i i i i i i i i i i _
    i i i i i i i i i i i i i i i i i i i i _
    i i i i i i i i i _
    "SATA Downshift Error Count", _             '183
    "End to End Error Det/Corr Count", _        '184
    i i i i i i i i i i i i i i i i i i i i _
    i i i i i i i i i i i i i i i i i i i i _
    i i i i i i i i i i i i i i i i i i i _
    i i i i i i i i i i _
    "Free Fall Sensor", _                       '254
     "" _
    }
    #undef i
    for n as long=1 to 255
        print n,sAttnam(n)
    next
    sleep
     
What is the sense in extra files for such an easy task?
jj2007
Posts: 2326
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: Initialising an array with many empty elements

Post by jj2007 »

Another option would be to load the array with data as strings 1...17, then to insert the empty strings:

Read sAttnam()
Insert sAttnam(14), 169 ' insert 169 empty strings; see Insert()
Insert sAttnam(185), 69


I searched the FB manual but could not find an INSERT command. Does it have a different name in FB?
fzabkar
Posts: 154
Joined: Sep 29, 2018 2:52
Location: Australia

Re: Initialising an array with many empty elements

Post by fzabkar »

Thanks everyone. The define/undefine idea looks the most appealing to me.

As for extra files, I prefer to keep everything in the one EXE file, including the usage instructions.

This is the present state of the rewrite (before I add the define/undefine edits):

http://www.users.on.net/~fzabkar/FreeBa ... smart5.bas
jj2007
Posts: 2326
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: Initialising an array with many empty elements

Post by jj2007 »

jj2007 wrote:Another option would be to load the array with data as strings 1...17, then to insert the empty strings:

Read sAttnam()
Insert sAttnam(14), 169 ' insert 169 empty strings; see Insert()
Insert sAttnam(185), 69


I searched the FB manual but could not find an INSERT command. Does it have a different name in FB?
Apparently there is not INSERT STRING command in FB, see this long thread. It is not clear if there was a solution.
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Initialising an array with many empty elements

Post by dodicat »

For a constant string you must write in each element manually.
Array insert is not a freebasic command, you must do your own routine, the array can be fixed length or var length here, but of cousre only fixed length can be initialised in {,,, . . . }

Code: Select all

'#define VarLen

#macro arrayinsert(a,index,insert)
    If index>=Lbound(a) And index<=Ubound(a)+1 Then
        Var index2=index-Lbound(a)
        #ifdef VarLen
        Redim Preserve a(Lbound(a) To  Ubound(a)+1)
        #endif
        For x As Integer= Ubound(a) To Lbound(a)+index2+1 Step -1
            Swap a(x),a(x-1)
        Next x
        a(Lbound(a)+index2)=insert
    End If
#endmacro


Dim sAttnam ( 1 To 255 ) As String => _
    { _
    "Raw Read Error Rate", _                    '1
    "Throughput Performance", _                 '2
    "Spin Up Time", _                           '3
    "Start/Stop Count", _                       '4
    "Reallocated Sector Count", _               '5
    "Read Channel Margin", _                    '6
    "Seek Error Rate", _                        '7
    "Seek Time Performance", _                  '8
    "Power-On Hours Count", _                   '9
    "Spin Retry Count", _                       '10
    "Drive Calibration Retry Count", _          '11
    "Drive Power Cycle Count", _                '12
    "Soft Read Error Rate"}                     '13
    for n as long=1 to 169
        arrayinsert(sAttnam,ubound(sAttnam),"")
    next n
    
      arrayinsert(sAttnam,183,"SATA Downshift Error Count")            '183
     arrayinsert(sAttnam,184,"End to End Error Det/Corr Count") 
     arrayinsert(sAttnam,254,"Free Fall Sensor")
  for n as long=1 to 255
        print n,sAttnam(n)
    next
    sleep
        
Some other basics have an insert method (power basic for example), and masm basic??
jj2007
Posts: 2326
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: Initialising an array with many empty elements

Post by jj2007 »

dodicat wrote:Some other basics have an insert method (power basic for example), and masm basic??
For string arrays: Insert, Delete, QSort, Recall, Store and many others; press Ctrl F in that page and search for Array
marcov
Posts: 3462
Joined: Jun 16, 2005 9:45
Location: Netherlands
Contact:

Re: Initialising an array with many empty elements

Post by marcov »

Or don't make an array of strings but an array of structs with an index and a string. To read a value, make a small func to binsearch the array for the index field to match x and then return the string
jj2007
Posts: 2326
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: Initialising an array with many empty elements

Post by jj2007 »

Sounds suspiciously close to a linked list ;-)
speedfixer
Posts: 606
Joined: Nov 28, 2012 1:27
Location: CA, USA moving to WA, USA
Contact:

Re: Initialising an array with many empty elements

Post by speedfixer »

If this is support for a distributed application:

Define/undefine would require shipping a different exe file to your customer to troubleshoot. Not good.
A resource data set could be enabled with a proper commandline tail provided to the customer when needed.

At least this would instill some extra confidence in your customer that you have:

Confidence (and humility) that you and your program are not perfect, and
you have provided a facility to inspire an attempt at a quick fix.


david
marcov
Posts: 3462
Joined: Jun 16, 2005 9:45
Location: Netherlands
Contact:

Re: Initialising an array with many empty elements

Post by marcov »

jj2007 wrote:Sounds suspiciously close to a linked list ;-)
Actually it is more like a tree mapped to an array, since it has O(log(n)) lookup behaviour.

But the point is that it can be instantiated statically (as a constant). I use such lookup tables a lot.
Post Reply