RE: Using Block instead of Module?
- To: mathgroup at smc.vnet.net
- Subject: [mg24268] RE: [mg24251] Using Block instead of Module?
- From: Wolf Hartmut <hwolf at debis.com>
- Date: Wed, 5 Jul 2000 23:10:50 -0400 (EDT)
- Sender: owner-wri-mathgroup at wolfram.com
> -----Original Message-----
> From: d8442803 at student.nsysu.edu.tw [SMTP:d8442803 at student.nsysu.edu.tw]
To: mathgroup at smc.vnet.net
> Sent: Tuesday, July 04, 2000 9:22 PM
> To: mathgroup at smc.vnet.net
> Subject: [mg24251] Using Block instead of Module?
>
> Dear listers,
>
> I try to output results to a file whose name is variable, and find Module
> statement cannot fulfill this purpose, only Block can do. For example,
>
> outputTest[lst_List, id_Integer] := Module[{fname, tmp = lst},
> fname = ToString[id] <> ".txt";
> ToExpression["tmp >>" <> fname];
> ];
>
> outputTest[{1, 2, 3}, 3]
>
> The output file is 3.txt, but the content is "tmp". But if I change
> Module statement into Block as below:
>
> outputTest[lst_List, id_Integer] := Block[{fname, tmp = lst},
> fname = ToString[id] <> ".txt";
> ToExpression["tmp >>" <> fname];
> ];
>
> outputTest[{1, 2, 3}, 3]
>
> The output file is 3.txt, and the content is correct, {1, 2, 3}. How if
> one used to use Module, like me, should do for the same purpose?
>
[Wolf Hartmut]
Hallo Wen-Feng,
the problem with Module in your case, was with the name of the temporary
variable. Within Module the variable tmp (and also fname for that matter)
are renamed to tmp$nnn and fname$nnn, however not when occurring in a text
string. So after ToExpression you effectively refer to the global variable
tmp (which presumable was undefined in your case).
Observe:
In[43]:= $ModuleNumber
Out[43]= 16
In[44]:=
outputTest[lst_List, id_Integer] :=
Module[{fname, tmp = lst}, fname = ToString[id] <> ".txt";
ToExpression["tmp$16 >>" <> fname];];
In[45]:= outputTest[{1, 2, 3}, 3]
In[48]:= !! 3.txt
>From In[48]:= {1, 2, 3}
In[49]:= outputTest[{4, 5, 6}, 3]
In[50]:= !! 3.txt
>From In[50]:= tmp$16
You see, here we carefully arrange the name, such that it worked for the
first call. It did no so for the second, because then $ModuleNumber was
incremented. Block works because there the variable name is not renamed.
Your problems however presumably arose with the special form for Put
expr >> filename
Therein filename is taken literally and automatically enclosed in
parentheses (I suppose by the frontend ???). All you have to do, is to avoid
that, e.g. (1) use Put:
In[54]:=
outputTest[lst_List, id_Integer] :=
Module[{fname, tmp = lst},
fname = "C:\\Temp\\" <> ToString[id] <> ".txt";
Put[tmp, fname];];
In[55]:= outputTest[{1, 5, 5}, 3]
In[56]:= !! "C:\\Temp\\3.txt"
>From In[56]:= {1, 5, 5}
or (2) use an expression which cannot be misunderstood as a literal
filename:
In[65]:=
outputTest[lst_List, id_Integer] := Module[{tmp = lst},
tmp >> "C:\\Temp\\" <> ToString[id] <> ".txt";];
In[66]:= outputTest[{7, 8, 9}, 5]
In[67]:= !! "C:\\Temp\\5.txt"
>From In[67]:= {7, 8, 9}
Kind regards, Hartmut