[Date Index]
[Thread Index]
[Author Index]
RE: If and openwrite and openappend
*To*: mathgroup at smc.vnet.net
*Subject*: [mg49216] RE: [mg49183] If and openwrite and openappend
*From*: "Wolf, Hartmut" <Hartmut.Wolf at t-systems.com>
*Date*: Thu, 8 Jul 2004 02:50:49 -0400 (EDT)
*Sender*: owner-wri-mathgroup at wolfram.com
>-----Original Message-----
>From: camartin at snet.net [mailto:camartin at snet.net]
To: mathgroup at smc.vnet.net
>Sent: Tuesday, July 06, 2004 9:34 AM
>To: mathgroup at smc.vnet.net
>Subject: [mg49216] [mg49183] If and openwrite and openappend
>
>
>MathGroup,
>
>Obviously I don't understand something about using If, etc.
>
>I'm trying to write a piece of code to store strings in a
>file. A very
>simplified example follows:
>
>Clear[t]
>Clear[nix]
>nix[x_String, y_String] :=
> t = {x, y}
>If[FileType["c:\\Mathematica\\dBaspap"] == None,
> stmp = OpenWrite["c:\\Mathematica\\dBaspap"],
> stmp = OpenAppend["c:\\Mathematica\\dBaspap"]]
>Write[stmp, t]
>Close[stmp]
>
>
>The function seems to work as I want if the file doesn't exist. If it
>exists the code doesn't work.
>
>If I the OpenAppend from the If statement things work fine. But a
>subsequent use of nix fails because it refuses to open a stream.
>
>I've read Wolfram fairly carefully I thought but I'm missing something
>obvious.
>Thanks for any help.
>
>Cliff Martin
>
>
>
If you look at your output, you'll see (when the file exists)
In[7]:=
If[FileType["c:\\temp\\dBaspap"] == None,
stmp = OpenWrite["c:\\temp\\dBaspap"],
stmp = OpenAppend["c:\\temp\\dBaspap"]]
Out[7]=
If[File == None, stmp = OpenWrite["c:\\temp\\dBaspap"],
stmp = OpenAppend["c:\\temp\\dBaspap"]]
and Help says:
"If[condition, t, f] is left unevaluated if condition evaluates to neither True nor False"
This is exactly what happened, as (when the File already existed):
In[11]:= FileType["c:\\temp\\dBaspap"]
Out[11]= File
Yet (surprise!)
In[12]:= File == None
Out[12]= File == None
The latter being neither True nor False. Now Help says, "lhs == rhs returns True if lhs and rhs are identical [or identical expressions]", but it doesn't return False if not so! Indeed it only "returns False, if lhs and rhs are determined to be unequal by comparisons between numbers or other raw data, such as strings".
This clearly is not the case here. (The expressions on both sides are symbols.)
What to do to compare two symbols? Follow the link at Help to SameQ:
"lhs === rhs yields True if the expression lhs is identical to rhs, and yields False otherwise."
So this is what you need.
Coming back to If, note (at Help again):
"If[condition, t, f, u] gives u if condition evaluates to neither True nor False."
So Mathematica's If has 3 branches: True, False and "cannot decide"
--
Hartmut Wolf
Prev by Date:
**Re: sorting polynomials by the degree of certain terms**
Next by Date:
**Re: Re: Changing the Natural Sort Order**
Previous by thread:
**Re: If and openwrite and openappend**
Next by thread:
**RE : Interpolating function**
| |