5

I need to understand the difference between MATREAD and READ? and please provide a simple example on where and how to use one.

TMAN-MAN
  • 141
  • 1
  • 2
  • 13

2 Answers2

9

READ will read a record from disk and return it as a Dynamic Array. MATREAD will read a record from disk and return it as a Dimensioned Array.

So, the real trick is to work out which array type is more applicable for your use case.

A Dynamic Array is essentially a string which uses certain byte markers to delimit each element (attribute/multivalue/subvalue). It is extremely easy to use, doesn't require upfront declaration or sizing. On the downside, if can be slower than a Dimensioned Array, mainly for large records or when you will be randomly retrieve attributes from the array - O(n log n) I think. Sequential access is optimized and is close to a Dimensioned Arrays speed.

A Dimensioned Array is essentially an array of strings (or Dynamic Arrays in the case of UniVerse). It reads each attribute into an array position. Each array position will then consist of a Dynamic Array for just the multivalue/subvalue positions of that attribute. Dimensioned Arrays required you to declare them and the number of array positions upfront. Depending on the flavor you have running, it will may cause a runtime error if you attempt to read in a record with more attributes than the array is sized for. On the flip side, it is O(1) to retrieve attributes from the array, regardless of your access pattern.

Dan McGrath
  • 41,220
  • 11
  • 99
  • 130
  • 2
    Dan - good explanation. I think dynamic array access is O(n), however. Whipped up a quick test and below are the results: `Time to access position: 50000 = 0 ms. Time to access position: 100000 = 1 ms. Time to access position: 200000 = 3 ms. Time to access position: 500000 = 8 ms. Time to access position: 1000000 = 15 ms. Time to access position: 5000000 = 76 ms. Time to access position: 10000000 = 153 ms.` The last couple examples show a pretty good linear progression. – Shawn McKnight May 15 '13 at 14:20
  • 1
    Thanks Shawn - you are correct. A single access is O(n). I was thinking about accessing all elements, which is O(n) for dimensioned and O(n^2) for dynamic. – Dan McGrath May 16 '13 at 04:09
  • Dan - just wanted to add this is a great answer. Very direct and well informed. – Austin S. Oct 15 '13 at 14:13
2

MATREADU bundles a MATPARSE with the READ. These blow the attributes of a dynamic array into a dimensioned or fixed array. This may be more efficient if you access the array frequently and more convenient if you are reading an I-type from a DICTionary. READV reads only one array attribute. Aim for clarity and optimize as required.

Here is an example in UniVerse of 4 lines producing the same output:

dim dimarr1(9), dimarr2(9)                                      
open 'VOC' else abort                                           
matread dimarr1 from 'OLDSTYLE' then print dimarr1(1) else abort
read dynarr from 'OLDSTYLE' then print dynarr<1> else abort     
matparse dimarr2 from dynarr ; print dimarr2(1)                 
readv dynatt from 'OLDSTYLE', 1 then print dynatt else abort    
end 
Ross Morrissey
  • 432
  • 3
  • 10