Re: using strings to create variables
- To: mathgroup at smc.vnet.net
- Subject: [mg38461] Re: using strings to create variables
- From: "Allan Hayes" <hay at haystack.demon.co.uk>
- Date: Wed, 18 Dec 2002 01:53:44 -0500 (EST)
- References: <atjv53$ls8$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
"Peter Dickof" <pdickof at sk.sympatico.ca> wrote in message news:atjv53$ls8$1 at smc.vnet.net... > I have a text file with a first line containing words giving the names > of the data in the column below. Subsequent lines contain the data: > x y z > 1 2 3 > 2 4 6 > 3 6 9 > > > I want a function that will read this file and and create lists > x={1,2,3} > y={2,4,6} > z={3,6,9} > > I do not understand how to take the strings "x", "y", and "z" that I > read from the first line and create lists with those names to which I > can assign the values. > > Peter > Peter, I hope that the following may help. Clear[x,y,z] expt={{x,y,z},{1,2,3},{4,5,6},{7,8,9}}; Export["file",expt,"Table"]; impt =Import["file", "Table"]; Check that we have strings in the first entry: InputForm[impt] {{"x", "y", "z"}, {1, 2, 3}, {4, 5, 6}, {7, 8, 9}} We cannot assign values to strings, so we must make them into symbols; and we need to force full evaluation of the left sided ot the following assignment Evaluate[ToExpression[impt[[1]]]]=Transpose[Rest[impt]]; Check {x,y,z} {{1,4,7},{2,5,8},{3,6,9}} Allan --------------------- Allan Hayes Mathematica Training and Consulting Leicester UK www.haystack.demon.co.uk hay at haystack.demon.co.uk Voice: +44 (0)116 271 4198 Fax: +44 (0)870 164 0565