Mathematica 9 is now available
Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2007
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2007

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

Search the Archive

RE: Block[], OpenRead[], & /@

  • To: mathgroup at smc.vnet.net
  • Subject: [mg75943] RE: [mg75862] Block[], OpenRead[], & /@
  • From: "David Annetts" <davidannetts at aapt.net.au>
  • Date: Mon, 14 May 2007 03:26:34 -0400 (EDT)
  • References: <200705120705.DAA23873@smc.vnet.net>

Hi Mary Beth,

> 
> files = FileNames["*txt"]
> 
> strm6 = OpenRead["Au6002.txt"]
> Skip[strm6, Record, 4, NullRecords -> False]       (*First 4 
> lines of each
> files contains non-numeric values, so skip these lines*)
> six= ReadList[strm6, Table[Number, {i, 1, 19}]]     (*Put the 
> data into a
> table called by a name*)
> Close[strm6]
> 
> 
> I would like to take automat this so that I am not copying 
> and pasting file names so I thought I could use the Block[] 
> funtion and then map the process onto all the file names in 
> the directory.  When I imput the following code:
> 
> Block[{str}, str = OpenRead[#]; Skip[str, Record, 4,
>    NullRecords -> False]; ReadList[
>     str, Table[Number, {i, 1, 19}]]; Close[str]] & /@ files
> 
> 
> my output is:
> {Au6001.txt, Au6002.txt, Au6003.txt, Au6004.txt, Au6005.txt}
> 
> I do not understand why I get the output I see above.  When I 
> modify the code above to:
> 
> 
> Block[{str}, str = OpenRead[#];
> Skip[str, Record, 4,
>       NullRecords -> False]; one = ReadList[
>       str, Table[Number, {i, 1, 19}]]; Close[str]] & /@ files
> 
> and ask for
> 
> one // TableForm
> 
> then I get the data for the last file in the list of file 
> names which indicates to me that I may be close in getting 
> this whole thing to work.

You're sort of close ....

The key is the Close[] statement.  Occuring as it does as the last line of
your block means that the Block returns only the output of Close -- the name
of the file that is being closed.

The easiest fix is to insert and extra line in your Block with the line
"one" ie

	Block[{str}, 
      str = OpenRead[#]; 
	Skip[str, Record, 4,   NullRecords -> False];
      one = ReadList[str, Table[Number, {i, 1, 19}]];
      Close[str];
      one] & /@ files

This is equivalent to 

	Block[{str}, 
      str = OpenRead[#]; 
	Skip[str, Record, 4,   NullRecords -> False];
      one = ReadList[str, Table[Number, {i, 1, 19}]];
      Close[str];
      Return[one]]& /@ files

which (to me anyway) is clearer.  Return[] is redundant and there is a
school of thought which holds that including it only encourages the use of
multiple paths from a Module (or Block).

Regards,

Dave.


  • Prev by Date: HoldForm
  • Next by Date: Re: Re: v.6 RevolutionPlot3D
  • Previous by thread: Block[], OpenRead[], & /@
  • Next by thread: Re: Block[], OpenRead[], & /@