Initialising an array with many empty elements

New to FreeBASIC? Post your questions here.
dodicat
Posts: 7976
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Initialising an array with many empty elements

Post by dodicat »

I don't see much benefit in wrapping an array in a udt (For this case)

Code: Select all

 

    
     Type array
         As  String s(1 To 255)
         As Long idx(1 To 255)
       Declare const Function find(As Long) As const String
         Declare Constructor()
     End Type
     
     Constructor array()
     static as long n
     if n=1 then print "Closed for business":return
     n+=1
     
     #define i "",
      Dim As  String g(1 To 255)= _
     { _
    "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
   this.s(n)=g(n)
   this.idx(n)=n
Next n
Erase g
     End Constructor
     
     
  function array.find(n as long) as const string
         return this.s(n)
    end function   
     
     
     
     Dim As array Temp
     Dim As Const array C=Temp
    
     
     
     
 
    print C.find(3)
    
    dim as array A

 
  
    Sleep
    
     
      
LawrenceG
Posts: 4
Joined: Mar 04, 2021 16:38

Re: Initialising an array with many empty elements

Post by LawrenceG »

What is the advantage of this over using a variable array, assigning null strings to every element with a for-next loop and then reassigning the few elements that have some other value?
Post Reply