Re: reading variable names and values from a file
- To: mathgroup at smc.vnet.net
- Subject: [mg41541] Re: reading variable names and values from a file
- From: Torben Winther Graversen <immtwg at erlang.gbar.dtu.dk>
- Date: Mon, 26 May 2003 05:46:21 -0400 (EDT)
- References: <bamuol$11q$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Chuck Wilkerson <cww at lanl.gov> wrote:
: Howdy!
: I would like to read a text file that looks like the following:
: a 1
: b 2
: c 3
: ...
: and end up having assigned the value in the second column to the
: variable name in the first column, i.e.
Hi,
Would it be easy to just modify the input file to have an equal sign
between each column and just use Get?
<< filename
If not, try:
Scan[Apply[Set,
Hold @@ MapAt[ToHeldExpression, #, {1}] /. Hold[s_]->s] &,
Import["input.txt", "Table"]]
Written as a handy function:
SetVarsFromFile[filename_] :=
Scan[Apply[Set,
Hold @@ MapAt[ToHeldExpression, #, {1}] /. Hold[s_]->s] &,
Import[filename, "Table"]]
Regards,
Torben