MathGroup Archive 2005

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

Search the Archive

Re: Reading in a file

  • To: mathgroup at smc.vnet.net
  • Subject: [mg57045] Re: [mg57024] Reading in a file
  • From: János <janos.lobb at yale.edu>
  • Date: Thu, 12 May 2005 22:44:29 -0400 (EDT)
  • References: <200505120634.CAA09012@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

On May 12, 2005, at 2:34 AM, Swati Shah wrote:

> Hi everyone,
> I have the following file named "temp.txt"
> with the following format:
>
> ;  Restart file created by make.m
> ;
> NYTOT        140
> STTOT         30
> ;
> TIME 0.00000000000000
> 3.1     4.1
> 2.3    4.5
> 3.3     2.5
> 1.1    4.1
>
> I want to read this file in, but store the value of NYTOT which is 140
> in a variable nytot, sttot in another variable named sttot
>
> At the end, I would like the get
> nytot = 140
> sttot = 30
> time = 0.0
>
> and the rest of the values in another list:
> tmpLst = {3.1,4.1,2.3,4.5,3.3,2.5,1.1,4.1}
>
> I have done the following so far:
>
> tmpLst=Flatten[Select[Import["temp.txt", "Table"], NumericQ[First 
> [#]] &]];
> Which gives the tmpLst values but am not sure how I can get the sttot,
> nytot and time values?
>
> Any ideas?
>
> Thanks in advance

One idea:

In[1]:=
SetDirectory[
   "~/Documents/Own solutions"]
Out[1]=
"/Volumes/Data/janos/Document\
s/Own solutions"

In[2]:=
files = FileNames["*.txt"]
Out[2]=
{"Reading from file.txt",
   "StringToNumbers.txt",
   "swati.txt", "usgs.txt"}

In[3]:=
fl = ReadList[files[[3]],
    Record]
Out[3]=
{";  Restart file created by \
make.m", ";",
   "NYTOT        140",
   "STTOT         30", ";",
   "TIME 0.00000000000000",
   "3.1     4.1",
   "2.3    4.5",
   "3.3     2.5", "1.1    4.1"}

In[4]:=
mapfl = (StringSplit[
      #1] & ) /@ fl
Out[4]=
{{";", "Restart", "file",
    "created", "by",
    "make.m"}, {";"},
   {"NYTOT", "140"},
   {"STTOT", "30"}, {";"},
   {"TIME",
    "0.00000000000000"},
   {"3.1", "4.1"},
   {"2.3", "4.5"},
   {"3.3", "2.5"},
   {"1.1", "4.1"}}


In[5]:=
nytot = ToExpression[
    Select[mapfl,
      #1[[1]] == "NYTOT" & ][[
     1,2]]]
Out[5]=
140

In[6]:=
sttot = ToExpression[
    Select[mapfl,
      #1[[1]] == "STTOT" & ][[
     1,2]]]
Out[6]=
30

In[7]:=
time = nytot = ToExpression[
     Select[mapfl,
       #1[[1]] == "TIME" & ][[
      1,2]]]
Out[7]=
0.

János

----------------------------------------------
Trying to argue with a politician is like lifting up the head of a  
corpse.
(S. Lem: His Master Voice)


  • Prev by Date: Re: GramSchmidt problem
  • Next by Date: FindRoot::nlnum error message for non-linear system of equations
  • Previous by thread: Reading in a file
  • Next by thread: Re: Reading in a file