Re: Structures (WAS Re: Memory)
- Posted by "Carl R. White" <C.R.White at SCM.BRAD.AC.UK> Jan 11, 1999
- 501 views
A solution [I've discussed before]: struct customer sequence name sequence phone integer age end struct customer A A = {"John Smith", "555-1212", 23} -- becomes: -- General declarations for this scenario constant new_natural = 0, new_char = 0, new_string = "" type natural(integer a) return (a >= 0) end type type char(integer a) return (a >= 0 and a <= 255) end type type string(sequence n) if not length(n) return 1 end if -- empty string legal for i = 1 to length(n) do if not char(n[i]) then return 0 end if end for return 1 end type -- The actual translation: global constant Name = 1, Phone = 2, Age = 3, new_customer = { new_string, new_string, new_natural } global type customer(sequence struct) string name string phone natural age if length(struct) != length(new_customer) return 0 end if -- Type check the fields. If any of these fail, they fail in their -- respective type checks rather than here... name = struct[Name] phone = struct[Phone] age = struct[Age] return 1 -- got this far? It's okay then :) end type customer A A = new_customer A[Name] = "John Smith" A[Age] = 23 A[Phone] = "KL5-1A1B" -- A subtype could be set up to define the exact -- specs for a phone number.(char x 3, hyphen, etc.) -- or: A = {"John Smith", "KL5-1A1B", 23} -- but this requires knowledge of the -- structure of 'customer'. -- Carl R White -- Final Year Computer Science at the University of Bradford E-mail...: cyrek- at -bigfoot.com -- Remove the hyphens before mailing. Ta :) URL......: http://www.bigfoot.com/~cyrek/ Ykk rnyllaqur rgiokc cea nyemdok ymc giququezka caysgr -- B.Q.Vgesa