MathGroup Archive 2000

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

Search the Archive

RE: Test for On/Off

  • To: mathgroup at smc.vnet.net
  • Subject: [mg24718] RE: [mg24689] Test for On/Off
  • From: Wolf Hartmut <hwolf at debis.com>
  • Date: Wed, 9 Aug 2000 02:31:16 -0400 (EDT)
  • Sender: owner-wri-mathgroup at wolfram.com


> -----Original Message-----
> From:	Adalbert Hanssen [SMTP:hanssen at zeiss.de]
To: mathgroup at smc.vnet.net
> Sent:	Friday, August 04, 2000 11:04 AM
> To:	mathgroup
> Subject:	Re: [mg24689] Test for On/Off
> 
> Hi, All!
> 
> If you say Off[message], then the message is wrapped in $Off:
> 
> In[13]:=On[General::spell]
> In[14]:=General::spell
> Out[14]="Possible spelling error: new symbol name \"`1`\" is 
>         similar to existing symbols `2`."
> 
> In[15]:=Off[General::spell]
> In[16]:=General::spell
> Out[16]=$Off["Possible spelling error: new symbol name \"`1`\" 
>         is similar to existing symbols `2`."]
> 
> In order to determine the On/Off state, we can investigate the 
> Head of the message:
> 
> In[17]:=On[General::spell]
> In[18]:=Head[General::spell]
> Out[18]=String
> 
> In[19]:=Off[General::spell]
> In[20]:=Head[General::spell]
> Out[20]=$Off
> 
> The message is Off, if the head of it is $Off. An implementation 
> of checking the current state would be:
> 
> In[43]:=
> CurrentState[message_]:=If[Head[Evaluate[message]]===$Off
>                           ,Off
>                           ,On
>                           ];
> Attributes[CurrentState]={HoldAll};
> 
> In[46]:=On[General::spell];
>         CurrentState[General::spell]
>         Off[General::spell];
>         CurrentState[General::spell]
> Out[47]=On
> Out[49]=Off
> 
> 
> Now, I would like to overload On[message] and Off[message], such 
> that they always return the previous state before On and Off were 
> applied. Any Idea how to overload On and Off?
> 
> Regards
> 
> Adalbert
> 
> 
[Hartmut Wolf]  

Hello Adalbert,

following your suggestion

 
In[1]:= Off[General::spell];

In[2]:= Attributes[CurrentState] = {HoldAll};
In[3]:= CurrentState[message_] := 
          If[Head[Evaluate[message]] === $Off, "Off", "On"]

On reasons that will be discussed soon, I output the strings, no symbols.

In[4]:= On[General::spell];
        CurrentState[General::spell]
        Off[General::spell];
        CurrentState[General::spell]
Out[5]= "On"
Out[7]= "Off"

Now we define Global versions for On, Off (standing on the shoulders of the
System functions):

 
In[12]:= SetAttributes[Global`On, HoldAll]
In[13]:= Global`On[message_] := 
    First[{CurrentState[message], System`On[message]}]


In[15]:= CurrentState[General::spell]
Out[15]= "Off"

In[16]:= On[General::spell]
Out[16]= "Off"
In[17]:= CurrentState[General::spell]
Out[17]= "On"
In[18]:= Message[General::spell, "**1**", "**2**"]
>From In[18]:=
General::"spell": "Possible spelling error: new symbol name
\"\!\(\"**1**\"\)\
\" is similar to existing symbols \!\(\"**2**\"\)."



In[19]:= SetAttributes[Global`Off, HoldAll]
In[20]:= Global`Off[message_] := 
    First[{CurrentState[message], System`Off[message]}]


In[22]:= Off[General::spell]
Out[22]= "On"

In[23]:= Message[General::spell, "**1**", "**2**"]

In[24]:= CurrentState[General::spell]
Out[24]= "Off"

In[25]:= Off[General::spell]
Out[25]= "Off"

In[26]:= On[General::spell]
Out[26]= "Off"

In[27]:= Message[General::spell, "**1**", "**2**"]
>From In[27]:=
General::"spell": "Possible spelling error: new symbol name
\"\!\(\"**1**\"\)\
\" is similar to existing symbols \!\(\"**2**\"\)."

In[28]:= On[General::spell]
Out[28]= "On"

Now, if we hadn't redefined the output of CurrentState to strings we would
have got System`On and System`Off, which disturbs a little. (This is
because, when CurrentState was defined there had been no global Symbols Off,
On. So after they had been defined Mathematica had to refer to the symbols
meant prior, i.e. those in the System context.)

So if you insist on returning the symbols, not strings, you have to define
the function CurrentState *after* the Global versions for On and Off

In[29]:= CurrentState[message_] := 
           If[Head[Evaluate[message]] === $Off, Off, On];

(this redefines CurrentState)

In[30]:= Off[General::spell]
Out[30]= On

In[31]:= On[General::spell]
Out[31]= Off

However I'd prefer the string version anyhow.

-- Hartmut



  • Prev by Date: Re: 2D array to tab delimited file
  • Next by Date: Re: Re: With[{software=Mathematica}, Frustration]
  • Previous by thread: Re: Test for On/Off
  • Next by thread: Re: Mathematica won't solve simple diff. eqn.--Correction