Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what's the difference between MATREAD and READ?

Tags:

universe

u2

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

like image 705
TMAN-MAN Avatar asked May 13 '13 11:05

TMAN-MAN


2 Answers

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.

like image 116
Dan McGrath Avatar answered Oct 14 '22 10:10

Dan McGrath


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 
like image 41
Ross Morrissey Avatar answered Oct 14 '22 12:10

Ross Morrissey