Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2006
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2006

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: dynamic variable definiton

  • To: mathgroup at smc.vnet.net
  • Subject: [mg67287] Re: dynamic variable definiton
  • From: Bill Rowe <readnewsciv at earthlink.net>
  • Date: Thu, 15 Jun 2006 03:27:06 -0400 (EDT)
  • Sender: owner-wri-mathgroup at wolfram.com

On 6/14/06 at 6:28 AM, tsariysk at craft-tech.com (T.Sariyski) wrote:

>Hi, I have a list of lists. The first element of each list is a
>description, the rest are numerical. Is it possible dynamic to
>create variable and to assign values to them? For example, I read
>data with ReadList:

>rawdata=ReadList["myfile.txt",Word,WordSeparators->"",RecordListsïâ??¢
>True,RecordSeparatorsïâ??¢{"\n\n","\n","\n"}];

>{{Face 1},{observ 0.1000E+02 0.3000E+01 0.4000E+01},{center
>0.1050E+02 0.3000E+01 0.4000E+01},{e1 0.1000E+01 0.0000E+00
>0.0000E+00},{e2 0.0000E+00 0.1000E+01 0.0000E+00},{e3 0.0000E+00
>0.0000E+00 0.1000E+01},{v1 0.1050E+02 0.3500E+01 0.4500E+01},{v2
>0.1050E+02 0.2500E+01 0.4500E+01},{v3 0.1050E+02 0.2500E+01
>0.4000E+01},{v4 0.1050E+02 0.3500E+01 0.4000E+01}}

>I would like to have:

>Face=1; obsrv={0.1000E+02 , 0.3000E+01, 0.4000E+01};
>center={0.1050E+02, 0.3000E+01, 0.4000E+01}; e1=... etc.

You can achieve what you want with the following:

str = {"observ 0.1000E+02 0.3000E+01 0.4000E+01"};

Here, since I don't have a copy of your file as an example, I simply set a variable to one of the lines in your post.

Now to convert the first item to a symbol and set it to a list of the remaining items

In[12]:=
Set@@({ToExpression@First@#,Rest@#}&@StringSplit[First@str]);

In[13]:=
observ

Out[13]=
{0.1000E+02,0.3000E+01,0.4000E+01}

A couple of caveats. This will not work if there observ already exists in the Global` context. And, this sets observ to a list of strings. Likely more useful is to set observ to a list of numbers which can be done as follows:

In[16]:=
Remove[observ]

In[17]:=
Set@@{ToExpression@StringTake[First@str,6],ReadList[StringToStream@StringDrop[\
First@str,6],Number]};

In[18]:=
observ

Out[18]=
{10.,3.,4.}

Note in this example, use of Remove so that the Set command would work correctly. Also note, this code assumes you know the length of the first item. This code could of course be encapsulated in a module and used StringLength to find the length of the first item, eliminating the need to know the length of the first item.

Finally, I suspect the whole operation could be made simpler by either using Import instead of ReadList or changing the options to ReadList. Using Import, it should be possible to have the first item be read in as a string and all of the numeric items read in as numbers. Assuming a structure such as 

{String, Number, ....}

for each record.

then

Set@@({ToExpression@First@#,Rest@#}&@record)

will do the trick. Again, this assumes a variable with the same name as the first item does not exist.
--
To reply via email subtract one hundred and four


  • Prev by Date: Re: Find a function that fits two dimensional data
  • Next by Date: [Fwd: Re: Re: new slide template: how change?]
  • Previous by thread: Re: dynamic variable definiton
  • Next by thread: Getting the (correct) roots of a polynomial