Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!usc!elroy.jpl.nasa.gov!ncar!gatech!wrdis01!nstn.ns.ca!clyde.concordia.ca!altitude!matrox!uvm-gen!kira!nic!mars!maine!io92203 From: IO92203@MAINE.BITNET (Scott Maxell) Newsgroups: comp.lang.pascal Subject: Re: need a little flexibility Message-ID: <91115.111155IO92203@MAINE.BITNET> Date: 25 Apr 91 15:11:55 GMT References: <1991Apr24.183315.7997@ux1.cso.uiuc.edu> <1991Apr24.235739.25115@watmath.waterloo.edu> Organization: University of Maine System Lines: 82 In article <1991Apr24.235739.25115@watmath.waterloo.edu>, nmouawad@watmath.waterloo.edu (Naji Mouawad) says: > >In article <1991Apr24.183315.7997@ux1.cso.uiuc.edu> amead@s.psych.uiuc.edu n >(ala >mead) writes: >>Hi. I want to manipulate a data structure in the cleanest way possible >>(after I write this, I'd like to minimize maintenance time). I've been >>working with something like this: >> >>Block = record >> x : byte; >> y : byte; >> ... >> data : array[1..252] of byte; >>end; >> ***... text deleted ...*** >>And a variant record requires an exhaustive listing of it's fields right? > >I don't understand your last statement, it seems that a record of >the following form: > >Block = record > x : byte; > y : byte; > case boolean of > True : (data : array[1..252] of byte); > False: (dataw: array[1..126] of word); >end; > >would do the trick. You could access just as easily data or dataw >as follows: > >Var B : block; > w : word; > >Begin > w := 12345; > b.data[i] := hi(w); > b.dataw[i] := w; >end. > This last approach won't work as stated because a variant record can only contain one of the possible fields declared. The field in the variant should also have a declared variable and not just a type. Type Block = RECORD X, Y : Byte; Case WordData : Boolean OF True : ( DataW : ARRAY [1..126] OF Word ); False : ( Data : ARRAY [1..252] OF Byte ); END; (* record *) VAR WordData : BOOLEAN; OneWord : Word; OneByte : Byte; BEGIN B.WordData := True; (* or false depending on what your data is. *) OneByte := ????; (* Your byte data. *) OneWord := ????; (* Your word data. *) n := ????; (* Array index. *) Case B.WordData OF True : B.Data [n] := OneByte; False : B.DataW [n] := OneWord; END; (* case *) END; When adding data to a variant record, there needs to be a value placed in the tag field, or you will generally get an error. //////////////////////////////////////////\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ +---------+ Scott Maxell -- Bitnet ->> IO92203 @ maine | | -- Internet ->> IO92203 @ maine.maine.edu | O | | | | "What I need is a computer that will do what I want it to +---------+ do, not what I tell it to do..."