Re: Import variable and data
- To: mathgroup at smc.vnet.net
- Subject: [mg32743] Re: Import variable and data
- From: boss at sigmasolns.com
- Date: Thu, 7 Feb 2002 05:12:27 -0500 (EST)
- Organization: Sigma Solutions
- References: <a3qr9f$29m$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Mark Van De Vyver wrote: > Hi MathGroup, > > I'm having trouble with the following variable and data import: > The import file is input.txt: > > a b c d > 1 23 45 678 > > inputData=Import[input.txt,"Table"] > {{a,b,c,d},{1,23,45,678}} > > This all works OK. > Head[..] indicates the a,b,c and d are strings, which is fine, because > ToExpression[..] changes them to symbols. What I'd like to do is use > inputData to do the following: > > {a,b,c,d}={1,23,45,678} > > However, if I try > > ToExpression[ inputData[[1]] ] = inputData[[2]] > > I get an error: > > Set::write: Tag ToExpression in ToExpression[a] is Protected. > > If I then enter 'a' in Mathematica I do not get 1 returned! No matter how I try I > can't work it so that when I enter the symbol 'a' in Mathematica I get the value > from inputData[[2]]. > > Not sure if that makes sense? Mark, You cannot make an assignment to a character string. You need to create an intermediate variable before making the assignment statement. However the answer is still not that easy. The behavior of Mathematica in this case is quite subtle. Let me give you one solution and chew on coming up with a good explanation for why Mathematica behaves the way it does. newData = Import["test.dat"] {{"a", "b", "c", "d"}, {1, 23, 45, 678}} lhs = ToExpression[newData[[1]]] {a, b, c, d} rhs = newData[[2]] {1, 23, 45, 678} MapThread[(#1 = #2) &, {lhs, rhs}] {1, 23, 45, 678} a 1 Sorry for any duplicate messages. Regards, Ed Boss -- Ed Boss Sigma Solutions: Where Science Meets Software Mathematica Training and Consulting http://www.sigmasolns.com/mathematica.html