Re: Simple List question. HELP.
- To: mathgroup at smc.vnet.net
- Subject: [mg39422] Re: Simple List question. HELP.
- From: Mark Westwood <MarkCWestwood at compuserve.com>
- Date: Fri, 14 Feb 2003 03:24:04 -0500 (EST)
- References: <b2fr48$kak$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Daniel In[533] in your example has lst[1]={{0,1}} which is a bit like defining f[1] = {{0,1}}. As a beginner you (and I) are best thinking of single square brackets as enclosing the arguments to a function. It is a valid technique, but not really what you should be using to build up a list. You could try lst[[1]]={{0,1}} (*Note the double square brackets which are used to enclose a part specification *) but you will find that lst[[2]]={{x,y}} fails, because lst[[2]] does not exist. Far better to use Append all the time. Note too that, like many a functional programming language Append[list, value] does NOT change list ! It returns the list made by appending value to list. What you probably want is: list = Append[list, value] that is, make an explicit assignment which causes the old version of list to be replaced by its new version, with value added. If you continue to Append in this way you'll probably notice a piling up of curly brackets. Don't worry about it. Wait until your list is finished, then Flatten it, and if necessary, Partition it again. I seem to remember reading a Technical Note to the effect that this is more efficient than maintaining the correctness of the list structure at every step. Hope this helps some Mark Westwood Daniel Heneghan wrote: > > I am new to Mathematica. This is vexing. All I want to do is create a > 2-dimensional list so that I can enter x,y values and then plot the > list. I want to do this programmatically. I am having such incredible > trouble trying to accomplish this simple task. I know that there is > probably a Mathematica optimized way to do this, but I and trying to > write a little program and for now I want to stay with paradigms that I > am familiar with. Here is what I have been doing. > > Create a 2 dimensional list. > In[532] lst={{}} > Out[532]= {{}} > > Enter the first pair into the first place in the list. > In[533]:= lst[1]={{0,1}} > > Errors and beeps here, but it does seem to record the correct values. > Set::write: Tag List in {{}}[1] is Protected. > Out[533]={{0,1}} > > Add anoter pair of values. > > In[534]:= lst=Append[lst,{{1,2}}] > Out[534]= {{},{{1,2}}} > The second pair is OK, but the first pair has been obliterated. > > Add another pair. Now all subsequent entries are OK, but I still have > lost the first pair. > In[535]:= lst=Append[lst,{{2,c}}] > Out[535]= {{},{{1,2}},{{2,c}}}-- > > What is going on? What are the mysteries of working with lists in > Mathematica. In any programming language this is simple. I can't grasp > it in Mathematica. The reason I need to do this is that for the list > plot I need the x values to start at 0 not 1. > > Thanks, > > Daniel Heneghan > Ceara Systems > (212) 696-9208 > ceara at bway.net