MathGroup Archive 2005

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

Search the Archive

Re: FileNames[] in a loop..

  • To: mathgroup at smc.vnet.net
  • Subject: [mg62084] Re: FileNames[] in a loop..
  • From: Antti Penttilä@smc.vnet.net
  • Date: Fri, 11 Nov 2005 02:51:47 -0500 (EST)
  • Organization: University of Helsinki
  • References: <dkuuru$7ga$1@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Uli Würfel wrote:
> Hello Experts,
> 
> I want to carry out the same calculation repeatedly, but with different
> sets of parameters. The parameters are located in files and I tried to use
> 
> dateien = FileNames["para*.txt"]
> 
> Then, I wanted to use a simple loop in the way:
> 
> For[i=1,i<=Length[dateien],
> {
>   str=OpenRead[dateien[[i]]];
>   ---- Reading of parameters ---
>   Close[str];
>   -----calculations---
> };
> i++]
> 
> Unfortunately, it does not work.
> Could you help?
> 
> Greetings,
> Uli
> 

The Mathematica help says that the structure of For is For[start,test,increment,body] and that the sequence of evaluation is test, body, incr. In your code, the actual calculation is in increment part, and i++ is the body. Therefore, the first time your calculation is evaluated, i has already value 2, and the last time i=Length[dateien]+1.

Change the code to

dateien = FileNames["para*.txt"]

Do[
 str=OpenRead[dateien[[i]]];
 ---- Reading of parameters ---'
 Close[str];
 -----calculations---,
{i, Length[dateien]}
]

-- 
   Antti Penttilä       Antti.I.Penttila at helsinki.fi.removethis


  • Prev by Date: Re: FileNames[] in a loop..
  • Next by Date: Re: ((a&&b)||c)==((a||c)&&(b||c))
  • Previous by thread: Re: FileNames[] in a loop..
  • Next by thread: Re: FileNames[] in a loop..