Re: Is it possible for me to implement associative arrays this way?
- To: mathgroup at smc.vnet.net
- Subject: [mg66196] Re: Is it possible for me to implement associative arrays this way?
- From: dh <dh at metrohm.ch>
- Date: Thu, 4 May 2006 05:19:36 -0400 (EDT)
- References: <e39k3l$cms$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Hi Don, you can always unprotect a symbol (command Unprotect), define some new rules for this symbol and re-protect it again (command Protect). However, I do not understand what you want to do with an "associative array". I understand this term to mean an un-ordered array. In this case this is much better implemented by a function. e.g. We want to assigne: 3->30 6->60 9->90 this is easily done by: f[3]=30; f[6]=60; f[9]=90 Daniel Don Taylor wrote: > I've tried searching the net to see if I could find an answer before > I bothered folks with this. > > I wrote a little bit of code to help someone out. It would have > been very convenient and clear if I could have used short lists as > subscripts, something similar to AWK's associative arrays, not what > Mathematica usually does with lists as subscripts. > > With experimenting I think I see how Mathematica uses > {1, 2, 3, 4, 5, 6}[[2]]; > {1, 2, 3, 4, 5, 6}[[{2,3,4}]]; > x={1, 2, 3, 4, 5, 6}; > x[[2]]; > x[[2]]=4; > x[[{2, 3, 4}]] = 5 > x[[{2, 3, 4}]] = {5, 4, 3} > and even a bit oddly > x[[{2, 3, 4}]] = {5, 4, 3, 2, 1} > But the the syntax > {1, 2, 3, 4, 5, 6}[[{{2,3,4}}]] > is not currently used by Mathematica. In other words when a Part > specification is not an integer or list of integers this isn't used. > > So, is there some way I could unprotect some bit of Mathematica, > insert a tiny bit of code such that if a Part specification was a > list of a list that it would create a table to implement the > associative array, and then reprotect Mathematica, for use both on > the left and right side of '='? > > This would be really convenient and not seem to subvert the > principles too much that Mathematica tries to use. > > Thank you >