Re: subscripts (Long Post)
- Posted by Lewis Townsend <keroltarr at HOTMAIL.COM> May 12, 2000
- 490 views
Hello Robert, >Sorry, I didn't really explain this well. >I meant that you could generalize the current notion >of what a sequence is, and turn it into a kind of >associative array, where any Euphoria object could >be associated with any Euphoria object. >The interpreter would maintain a list like: > >sequence a: > >subscript value > >1 0 >2 {0,0,{9},1} >3 {} >"Lewis" 99 >"Townsend" {1, 2, 3} Hmm, this would indeed be a powerful syntax addition. If the left side were to include variable names rather than sequences, I would like it even more. But then we get into structures where we can define private elements of a structure by names rather than by value: type human (sequence x) Name = human [1] FirstName = human [Name][1] LastName = human [Name][2] Age = human [2] end type human Lewis Lewis [Name] = {"Lewis", "Townsend"} Lewis [Age] = 21 puts (1, Lewis [FirstName]) -- prints "Lewis" >When you read a subscript, the interpreter would >find that subscript in the first column and return the >associated value in the second column. >Similarly, you could overwrite a subscripted value with >a[1] = 10 >or, >a["Townsend"] = {1,2,4} How would these things be set? Like my above example or otherwise? Would the associated values also have a normal integer value? sequence a a ["First"] = 5 ? a[1] -- is this equal to ? a["First"] -- this? If not, then what about the length() function? are the special subcripts included? do they have an equivalent numeric subscript? >I guess the values could also be generalized sequences >so you might see code like: >a["Rob"]["Craig"] = 9.9 >i.e. a subscript could be any Euphoria object, but it >only does one level of subscripting. Perl "hashes" >are a primitive version of what I've described here. > >As I said, I'm not really convinced that it would be >worth it to add all this stuff, either your proposal >or the one above. Well, my suggestion would be much simpler than associative arrays, I think, and still have most of the functionality. This way I could store a whole subscript string in a variable instead of having to use a bunch of integer constants as we do now. Lets use my classic example ;) Me = {{"Lewis","Townsend"}, 21} constant Name = 1 LastName = Name & 2 LI = LastName & 1 puts (1, Me [LI]) -- only 1 subscript needed -- instead of how we do it now Me = {{"Lewis","Townsend"}, 21} constant Name = 1, Last = 2, Initial = 1 -- three subscripts needed to puts (1, Me [Name] [Last] [Initial]) -- print my last initial I think it would be worth it. I know I for one would use this feature extensively if it were added and not just for OOP emulation either. I have a tendancy to use sequences a lot, maybe its because they make a lot more sense than just using lots of loose atoms. Also, when your data is variable, shrinking, and growing all the time, nothing else makes sense. At various times in the past I have made such extensive use of sequences and subscripts that I ended up confusing my self. Lets use my Stars of Conflict game that I have been working on indefinitely. Here is how I originally did it because it made more sense organizationally but became a jungle of subscripts: -- a space ship object structure {Name, Position {X, Y}, Direction {Facing, Moving {X, Y}}, Engine {Acceleration, MaxSpeed, TurningSpeed}, Offense {WeaponType, Ammo}, Defense {Life {MaxLife, CrntLife}, Armor {Protection, ArmorLife}}, etc... and this was just one ship in a sequence that represented all the ships in action. What if I want to access ship x's current life amount to test if it is less than zero: if ships [x][Defense][Life][CrntLife] <= 0 then explode (x) end if I quickly realized that all this confusing subscripting could be minimized if I broke down my organization as follows. {Name, Position {X, Y}, Direction {X,Y}, Facing, Acceleration, MaxSpeed, TurningSpeed, WeaponType, Amo, Life, MaxLife, Armor, ArmorLife}, Now I have many more elements but much less complexity to access those elements: if ships [x][Life] <= 0 then explode (x) end if This works fine but it hinders any changes I want to make. If I want to add another category then I either tack it on the end without regard to orgainizational pattern or I insert it where it fits best and then have to change all the constant subscript values for everything after that. The fewer number of elements we use, the eaiser the code is to update and maintain but it also gets complicated to use with several levels of subscription that each have to be coded in every time it is needed. I would rather be able to define the subscript structure once, in a well organized fashion and still be able to access them in a concise, straightforward, way. I hope my examples urge you reconsider your feelings about adding this feature. I think it would be extremely useful. Note that I respect your reluctance to add anything that isn't really useful. I, however, think that my proposed freature is not only useful but necessary in some cases. We have the sequence. It is infinitely flexible in its contents. However, there is a limit to the flexibility of its access mechanism. Sure, we can access any element of a sequence using an integer and a couple square brackets but what if THAT element is a sequence itself, and it can be. Well we can tack on another susbcript for that next level and so on. But what if we are handed a sequence whose complexity we don't know? Well we have to write a recursive routine to find the structure of that sequence. And then what do we have? Even if we save a sequence on how to get to a specific place in a sequence all we can do is read it out while we are there. We can't actually use that structure to subscript that sequence or any other sequence. We CAN use a stack of ifs but even then we can only guess at how deep the sequence will be and we still need a 2 or 3 lines of code for each possible level: a = sequence_of_unknown_lenght_or_structure sbscrpt = sequence_to_subscript_one_value_from_a if length (sbscrpt) = 1 then return a [sbscrpt[1]] elsif length (sbscrpt) = 2 then return a [sbscrpt[1]][sbscrpt[2]] elsif length (sbscrpt) = 2 then return a [sbscrpt[1]][sbscrpt[2]][sbscrpt[3]] elsif length (sbscrpt) = 2 then return a [sbscrpt[1]][sbscrpt[2]][sbscrpt[3]][sbscrpt[4]] end if -- Notice how it gets bigger all the time. I think I've written enough for today, let me know what you think or if there is a flaw in my reasoning or if you don't understand what I'm talking about ;) later, . __ _____ __ __ __ __ _____ . /\ \ /\ \|\ \ /\ \ /\ \ /\ \ /\ \ . / \_\ / \____\ \_\/ \_\/ \_\/ \_\/ \____\ . / / / / / ___/ | | / | / / / /\ / __ \ . / / / / / /_\ | | | / | / / / /\_\/ /_ \/ ./ / /\ / / ___/ | | |/ | / / / /\ \ \__ \ .\ / /__\\ / /__\ \ | /| |/ /\ / / \/\_\/ / . \/_____/ \/_____/ \|___/\|___/ \/_/ \_____/ keroltarr at hotmail.com http://geocities.com/keroltarr/ ________________________________________________________________________ Get Your Private, Free E-mail from MSN Hotmail at http://www.hotmail.com