Re: How To Change A Rule
- To: mathgroup at smc.vnet.net
- Subject: [mg32350] Re: How To Change A Rule
- From: Detlef Mueller <dmueller at mathematik.uni-kassel.de>
- Date: Wed, 16 Jan 2002 03:30:01 -0500 (EST)
- Organization: University of Kassel - Germany
- References: <a20mon$4hf$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Ariel Fligler wrote:
>
> Greetings,
>
> I'm quite newby to Mathematica programming and will thank you if you could
> help me solve the following:
>
> I have a list of rules of the form:
> {s1->-11, s2->12}
> And would like to change every rule in the list that it's RHS is negative to
> 0 so that I will get:
> {s1->0, s2->12}
> I tried to find an access function to manipulate rules but could not.
>
You can apply rules on a list of rules:
{s1->-11, s2->12}/.{x_/;x<0->0}
does the Job
(x_ stands for any Pattern and names it with "x",
the condition /;x<0 ensures, that only the negative
RHS are replaced).
{s1 -> -11, s2 -> 12, s3 -> -PI, s4 -> 3} /. {x_ /; x < 0 -> 0}
leads to
{s1 -> 0, s2 -> 12, s3 -> 0, s4 -> 3} as desired.
But
{-5 -> u, s1 -> -11, s2 -> 12, s3 -> -PI, s4 -> 3}
/. {x_ /; x < 0 -> 0}
yields:
{0 -> u, s1 -> 0, s2 -> 12, s3 -> 0, s4 -> 3}
So we specify the pattern more detailed:
{-5 -> u, s1 -> -11, s2 -> 12, s3 -> -PI, s4 -> 3}
/.{(x_ -> y_ /; y < 0) -> (x -> 0)}
resulting in
{-5 -> u, s1 -> 0, s2 -> 12, s3 -> -PI, s4 -> 3}
Why -PI isn't changed here, but in the first
version?
I don't know.
Greetings
Detlef